components/
tickv.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component for TicKV KV System Driver.
//!
//! This provides one component, TicKVComponent, which provides
//! a system call inteface to non-volatile storage.
//!
//! Usage
//! -----
//! ```rust
//!    let flash_ctrl_read_buf = static_init!(
//!        [u8; lowrisc::flash_ctrl::PAGE_SIZE],
//!        [0; lowrisc::flash_ctrl::PAGE_SIZE]
//!    );
//!    let page_buffer = static_init!(
//!        lowrisc::flash_ctrl::LowRiscPage,
//!        lowrisc::flash_ctrl::LowRiscPage::default()
//!    );
//!
//!    let mux_flash = components::tickv::FlashMuxComponent::new(&peripherals.flash_ctrl).finalize(
//!        components::flash_user_component_static!(lowrisc::flash_ctrl::FlashCtrl),
//!    );
//!
//!    // SipHash
//!    let sip_hash = static_init!(
//!        capsules_extra::sip_hash::SipHasher24,
//!        capsules_extra::sip_hash::SipHasher24::new()
//!    );
//!    sip_hash.register();
//!
//!    let tickv = components::tickv::TicKVComponent::new(
//!        sip_hash,
//!        &mux_flash,
//!        0x20040000 / lowrisc::flash_ctrl::PAGE_SIZE,
//!        0x40000,
//!        flash_ctrl_read_buf,
//!        page_buffer,
//!    )
//!    .finalize(components::tickv_component_static!(
//!        lowrisc::flash_ctrl::FlashCtrl,
//!        capsules_extra::sip_hash::SipHasher24
//!    ));
//!    hil::flash::HasClient::set_client(&peripherals.flash_ctrl, mux_flash);
//! ```

use capsules_core::virtualizers::virtual_flash::FlashUser;
use capsules_core::virtualizers::virtual_flash::MuxFlash;
use capsules_extra::tickv::TicKVSystem;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;
use kernel::hil::flash::HasClient;
use kernel::hil::hasher::Hasher;

// Setup static space for the objects.
#[macro_export]
macro_rules! tickv_component_static {
    ($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
        let flash =
            kernel::static_buf!(capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>);
        let tickv = kernel::static_buf!(
            capsules_extra::tickv::TicKVSystem<
                'static,
                capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>,
                $H,
                $PAGE_SIZE,
            >
        );

        (flash, tickv)
    };};
}

#[macro_export]
macro_rules! tickv_dedicated_flash_component_static {
    ($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
        let tickfs_read_buffer = kernel::static_buf!([u8; $PAGE_SIZE]);
        let tickv =
            kernel::static_buf!(capsules_extra::tickv::TicKVSystem<'static, $F, $H, $PAGE_SIZE>);

        (tickv, tickfs_read_buffer)
    };};
}

pub struct TicKVComponent<
    F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
    H: 'static + Hasher<'static, 8>,
    const PAGE_SIZE: usize,
> {
    mux_flash: &'static MuxFlash<'static, F>,
    hasher: &'static H,
    region_offset: usize,
    flash_size: usize,
    tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
    flash_read_buffer: &'static mut F::Page,
}

impl<
        F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
        H: Hasher<'static, 8>,
        const PAGE_SIZE: usize,
    > TicKVComponent<F, H, PAGE_SIZE>
{
    pub fn new(
        hasher: &'static H,
        mux_flash: &'static MuxFlash<'static, F>,
        region_offset: usize,
        flash_size: usize,
        tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
        flash_read_buffer: &'static mut F::Page,
    ) -> Self {
        Self {
            mux_flash,
            hasher,
            region_offset,
            flash_size,
            tickfs_read_buf,
            flash_read_buffer,
        }
    }
}

impl<
        F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
        H: 'static + Hasher<'static, 8>,
        const PAGE_SIZE: usize,
    > Component for TicKVComponent<F, H, PAGE_SIZE>
{
    type StaticInput = (
        &'static mut MaybeUninit<FlashUser<'static, F>>,
        &'static mut MaybeUninit<TicKVSystem<'static, FlashUser<'static, F>, H, PAGE_SIZE>>,
    );
    type Output = &'static TicKVSystem<'static, FlashUser<'static, F>, H, PAGE_SIZE>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let virtual_flash = static_buffer.0.write(FlashUser::new(self.mux_flash));

        let driver = static_buffer.1.write(TicKVSystem::new(
            virtual_flash,
            self.hasher,
            self.tickfs_read_buf,
            self.flash_read_buffer,
            self.region_offset,
            self.flash_size,
        ));
        virtual_flash.set_client(driver);
        driver.initialise();
        driver
    }
}

pub type TicKVDedicatedFlashComponentType<F, H, const PAGE: usize> =
    capsules_extra::tickv::TicKVSystem<'static, F, H, PAGE>;

pub struct TicKVDedicatedFlashComponent<
    F: 'static
        + hil::flash::Flash
        + hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
    H: 'static + Hasher<'static, 8>,
    const PAGE_SIZE: usize,
> {
    flash: &'static F,
    hasher: &'static H,
    region_offset: usize,
    flash_size: usize,
    flash_read_buffer: &'static mut F::Page,
}

impl<
        F: 'static
            + hil::flash::Flash
            + hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
        H: Hasher<'static, 8>,
        const PAGE_SIZE: usize,
    > TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
    pub fn new(
        hasher: &'static H,
        flash: &'static F,
        region_offset: usize,
        flash_size: usize,
        flash_read_buffer: &'static mut F::Page,
    ) -> Self {
        Self {
            flash,
            hasher,
            region_offset,
            flash_size,
            flash_read_buffer,
        }
    }
}

impl<
        F: 'static
            + hil::flash::Flash
            + hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
        H: 'static + Hasher<'static, 8>,
        const PAGE_SIZE: usize,
    > Component for TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
    type StaticInput = (
        &'static mut MaybeUninit<TicKVSystem<'static, F, H, PAGE_SIZE>>,
        &'static mut MaybeUninit<[u8; PAGE_SIZE]>,
    );
    type Output = &'static TicKVSystem<'static, F, H, PAGE_SIZE>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let tickfs_read_buf = static_buffer.1.write([0; PAGE_SIZE]);

        let tickv = static_buffer.0.write(TicKVSystem::new(
            self.flash,
            self.hasher,
            tickfs_read_buf,
            self.flash_read_buffer,
            self.region_offset,
            self.flash_size,
        ));
        self.flash.set_client(tickv);
        self.hasher.set_client(tickv);
        tickv.initialise();
        tickv
    }
}