Skip to content

Commit e7cb61b

Browse files
committed
Refactoring
- Don't wildcard-import from arch modules. Make it explicit. - Put translation table code into its own module. - Put boot code in boot.rs instead of cpu.rs - Other minor changes, most memory subsystem.
1 parent e815b34 commit e7cb61b

File tree

266 files changed

+6393
-4254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+6393
-4254
lines changed

00_before_we_start/README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ the tutorials advance.
1212

1313
Have fun!
1414

15-
## Code organization and architecture
15+
# Code organization and architecture
1616

1717
The code is divided into different *modules*, each representing a typical **subsystem** of the
1818
`kernel`. Top-level module files of subsystems reside directly in the `src` folder. For example,
@@ -25,15 +25,22 @@ architecture. For each supported processor architecture, there exists a subfolde
2525
for example, `src/_arch/aarch64`.
2626

2727
The architecture folders mirror the subsystem modules laid out in `src`. For example, architectural
28-
code that belongs to the `kernel`'s memory subsystem (`src/memory.rs`) would go into
29-
`src/_arch/aarch64/memory.rs`. The latter file is directly included and re-exported in
30-
`src/memory.rs`, so that the architectural code parts are transparent with respect to the code's
31-
module organization. That means a public function `foo()` defined in `src/_arch/aarch64/memory.rs`
32-
would be reachable as `crate::memory::foo()` only.
28+
code that belongs to the `kernel`'s MMU subsystem (`src/memory/mmu.rs`) would go into
29+
`src/_arch/aarch64/memory/mmu.rs`. The latter file is loaded as a module in `src/memory/mmu.rs`
30+
using the `path attribute`. Usually, the chosen module name is the generic module's name prefixed
31+
with `arch_`.
3332

34-
The `_` in `_arch` denotes that this folder is not part of the standard module hierarchy. Rather,
35-
it's contents are conditionally pulled into respective files using the `#[path =
36-
"_arch/xxx/yyy.rs"]` attribute.
33+
For example, this is the top of `src/memory/mmu.rs`:
34+
35+
```
36+
#[cfg(target_arch = "aarch64")]
37+
#[path = "../_arch/aarch64/memory/mmu.rs"]
38+
mod arch_mmu;
39+
```
40+
41+
Often times, items from the `arch_ module` will be publicly reexported by the parent module. This
42+
way, each architecture specific module can provide its implementation of an item, while the caller
43+
must not be concerned which architecture has been conditionally compiled.
3744

3845
## BSP code
3946

@@ -42,9 +49,8 @@ target board specific definitions and functions. These are things such as the bo
4249
instances of drivers for devices that are featured on the respective board.
4350

4451
Just like processor architecture code, the `BSP` code's module structure tries to mirror the
45-
`kernel`'s subsystem modules, but there is no transparent re-exporting this time. That means
46-
whatever is provided must be called starting from the `bsp` namespace, e.g.
47-
`bsp::driver::driver_manager()`.
52+
`kernel`'s subsystem modules, but there is no reexporting this time. That means whatever is provided
53+
must be called starting from the `bsp` namespace, e.g. `bsp::driver::driver_manager()`.
4854

4955
## Kernel interfaces
5056

@@ -96,3 +102,10 @@ From a namespace perspective, **memory** subsystem code lives in:
96102

97103
- `crate::memory::*`
98104
- `crate::bsp::memory::*`
105+
106+
# Boot flow
107+
108+
1. The kernel's entry point is the function `cpu::boot::arch_boot::_start()`.
109+
- It is implemented in `src/_arch/__arch_name__/cpu/boot.rs`.
110+
111+

01_wait_forever/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ export LINKER_FILE
3434
RUSTFLAGS = -C link-arg=-T$(LINKER_FILE) $(RUSTC_MISC_ARGS)
3535
RUSTFLAGS_PEDANTIC = $(RUSTFLAGS) -D warnings -D missing_docs
3636

37+
FEATURES = bsp_$(BSP)
3738
COMPILER_ARGS = --target=$(TARGET) \
38-
--features bsp_$(BSP) \
39+
--features $(FEATURES) \
3940
--release
4041

4142
RUSTC_CMD = cargo rustc $(COMPILER_ARGS)

01_wait_forever/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
- Only `.text` section.
2424
- `main.rs`: Important [inner attributes]:
2525
- `#![no_std]`, `#![no_main]`
26-
- `cpu.S`: Assembly `_start()` function that executes `wfe` (Wait For Event), halting all cores that
27-
are executing `_start()`.
26+
- `boot.S`: Assembly `_start()` function that executes `wfe` (Wait For Event), halting all cores
27+
that are executing `_start()`.
2828
- We (have to) define a `#[panic_handler]` function to make the compiler happy.
2929
- Make it `unimplemented!()` because it will be stripped out since it is not used.
3030

01_wait_forever/src/_arch/aarch64/cpu.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
//
3+
// Copyright (c) 2021 Andre Richter <andre.o.richter@gmail.com>
4+
5+
//! Architectural boot code.
6+
//!
7+
//! # Orientation
8+
//!
9+
//! Since arch modules are imported into generic modules using the path attribute, the path of this
10+
//! file is:
11+
//!
12+
//! crate::cpu::boot::arch_boot
13+
14+
// Assembly counterpart to this file. Includes function _start().
15+
global_asm!(include_str!("boot.S"));

01_wait_forever/src/bsp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Copyright (c) 2018-2021 Andre Richter <andre.o.richter@gmail.com>
44

5-
//! Conditional re-exporting of Board Support Packages.
5+
//! Conditional reexporting of Board Support Packages.
66
77
#[cfg(any(feature = "bsp_rpi3", feature = "bsp_rpi4"))]
88
mod raspberrypi;

01_wait_forever/src/cpu.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@
44

55
//! Processor code.
66
7-
#[cfg(target_arch = "aarch64")]
8-
#[path = "_arch/aarch64/cpu.rs"]
9-
mod arch_cpu;
10-
pub use arch_cpu::*;
7+
mod boot;

01_wait_forever/src/cpu/boot.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
//
3+
// Copyright (c) 2021 Andre Richter <andre.o.richter@gmail.com>
4+
5+
//! Boot code.
6+
7+
#[cfg(target_arch = "aarch64")]
8+
#[path = "../_arch/aarch64/cpu/boot.rs"]
9+
mod arch_boot;

01_wait_forever/src/main.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@
2020
//! `src/_arch`, for example, `src/_arch/aarch64`.
2121
//!
2222
//! The architecture folders mirror the subsystem modules laid out in `src`. For example,
23-
//! architectural code that belongs to the `kernel`'s memory subsystem (`src/memory.rs`) would go
24-
//! into `src/_arch/aarch64/memory.rs`. The latter file is directly included and re-exported in
25-
//! `src/memory.rs`, so that the architectural code parts are transparent with respect to the code's
26-
//! module organization. That means a public function `foo()` defined in
27-
//! `src/_arch/aarch64/memory.rs` would be reachable as `crate::memory::foo()` only.
23+
//! architectural code that belongs to the `kernel`'s MMU subsystem (`src/memory/mmu.rs`) would go
24+
//! into `src/_arch/aarch64/memory/mmu.rs`. The latter file is loaded as a module in
25+
//! `src/memory/mmu.rs` using the `path attribute`. Usually, the chosen module name is the generic
26+
//! module's name prefixed with `arch_`.
2827
//!
29-
//! The `_` in `_arch` denotes that this folder is not part of the standard module hierarchy.
30-
//! Rather, it's contents are conditionally pulled into respective files using the `#[path =
31-
//! "_arch/xxx/yyy.rs"]` attribute.
28+
//! For example, this is the top of `src/memory/mmu.rs`:
29+
//!
30+
//! ```
31+
//! #[cfg(target_arch = "aarch64")]
32+
//! #[path = "../_arch/aarch64/memory/mmu.rs"]
33+
//! mod arch_mmu;
34+
//! ```
35+
//!
36+
//! Often times, items from the `arch_ module` will be publicly reexported by the parent module.
37+
//! This way, each architecture specific module can provide its implementation of an item, while the
38+
//! caller must not be concerned which architecture has been conditionally compiled.
3239
//!
3340
//! ## BSP code
3441
//!
@@ -37,9 +44,8 @@
3744
//! or instances of drivers for devices that are featured on the respective board.
3845
//!
3946
//! Just like processor architecture code, the `BSP` code's module structure tries to mirror the
40-
//! `kernel`'s subsystem modules, but there is no transparent re-exporting this time. That means
41-
//! whatever is provided must be called starting from the `bsp` namespace, e.g.
42-
//! `bsp::driver::driver_manager()`.
47+
//! `kernel`'s subsystem modules, but there is no reexporting this time. That means whatever is
48+
//! provided must be called starting from the `bsp` namespace, e.g. `bsp::driver::driver_manager()`.
4349
//!
4450
//! ## Kernel interfaces
4551
//!
@@ -91,14 +97,17 @@
9197
//!
9298
//! - `crate::memory::*`
9399
//! - `crate::bsp::memory::*`
100+
//!
101+
//! # Boot flow
102+
//!
103+
//! 1. The kernel's entry point is the function `cpu::boot::arch_boot::_start()`.
104+
//! - It is implemented in `src/_arch/__arch_name__/cpu/boot.S`.
94105
95106
#![feature(asm)]
96107
#![feature(global_asm)]
97108
#![no_main]
98109
#![no_std]
99110

100-
// `mod cpu` provides the `_start()` function, the first function to run.
101-
102111
mod bsp;
103112
mod cpu;
104113
mod panic_wait;

0 commit comments

Comments
 (0)