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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#![cfg_attr(feature = "std", allow(unreachable_code, unused_variables))]

use crate::sv::{SvCall, SvService};
use core::{intrinsics::unreachable, mem::size_of};

/// A service to switch to a process stack.
///
/// See [`Switch::switch_context`] for details.
pub struct SwitchContextService {
    stack_ptr: *mut *const u8,
    data_ptr: *mut u8,
}

/// A service to switch back from a process stack.
///
/// See [`Switch::switch_back`] for details.
pub struct SwitchBackService {
    data_ptr: *mut *mut u8,
    data_size: usize,
}

/// Extends [`Supervisor`](crate::sv::Supervisor) types with
/// [`switch_context`](Switch::switch_context) and
/// [`switch_back`](Switch::switch_back) methods.
pub trait Switch<T>
where
    Self: SvCall<SwitchContextService>,
    Self: SvCall<SwitchBackService>,
{
    /// Switches to the given process stack.
    ///
    /// # Safety
    ///
    /// * `data` must be word-aligned.
    /// * `*stack_ptr` must be word-aligned.
    unsafe fn switch_context(data: *mut T, stack_ptr: *mut *const u8);

    /// Switches to the previous stack.
    ///
    /// # Safety
    ///
    /// * Must be called only from Process Stack.
    /// * `T` must match the previous [`switch_context`](Switch::switch_context).
    /// * `*data` must be word-aligned.
    unsafe fn switch_back(data: *mut *mut T);
}

unsafe impl Send for SwitchContextService {}
unsafe impl Send for SwitchBackService {}

impl SvService for SwitchContextService {
    unsafe extern "C" fn handler(&mut self) {
        #[cfg(feature = "std")]
        return unimplemented!();
        let Self {
            stack_ptr,
            data_ptr,
        } = *self;
        #[cfg(all(
            feature = "fpu",
            any(cortex_m_core = "cortex_m4f_r0p0", cortex_m_core = "cortex_m4f_r0p1")
        ))]
        asm!("
            mrs      r3, control
            tst      lr, #0x4
            bne      3f
            tst      lr, #0x10
            it       eq
            vstmdbeq sp!, {s16-s31}
            stmdb    sp!, {r3, r4-r11}
        0:
            ldr      r2, [r0]
            ldmia    r2!, {r3}
            push     {r0, r1, r3, lr}
            cmp      r3, #0
            bne      2f
        1:
            ldmia    r2!, {r3, r4-r11, lr}
            tst      lr, #0x10
            it       eq
            vldmiaeq r2!, {s16-s31}
            msr      psp, r2
            msr      control, r3
            bx       lr
        2:
            movw     r0, #0xED9C
            movt     r0, #0xE000
            ldmia    r3!, {r4-r11}
            stmia    r0, {r4-r11}
            ldmia    r3!, {r4-r11}
            stmia    r0, {r4-r11}
            mov      r3, #5
            str      r3, [r0, #-8]
            b        1b
        3:
            mrs      r2, psp
            tst      lr, #0x10
            it       eq
            vstmdbeq r2!, {s16-s31}
            stmdb    r2!, {r3, r4-r11}
            ldr      r3, [sp]
            str      r2, [r3]
            b        0b
        "   :
            : "{r0}"(stack_ptr), "{r1}"(data_ptr)
            : "cc", "memory"
            : "volatile"
        );
        #[cfg(not(all(
            feature = "fpu",
            any(cortex_m_core = "cortex_m4f_r0p0", cortex_m_core = "cortex_m4f_r0p1")
        )))]
        asm!("
            mrs      r3, control
            tst      lr, #0x4
            bne      3f
            stmdb    sp!, {r3, r4-r11}
        0:
            ldr      r2, [r0]
            ldmia    r2!, {r3}
            push     {r0, r1, r3, lr}
            cmp      r3, #0
            bne      2f
        1:
            ldmia    r2!, {r3, r4-r11, lr}
            msr      psp, r2
            msr      control, r3
            bx       lr
        2:
            movw     r0, #0xED9C
            movt     r0, #0xE000
            ldmia    r3!, {r4-r11}
            stmia    r0, {r4-r11}
            ldmia    r3!, {r4-r11}
            stmia    r0, {r4-r11}
            mov      r3, #5
            str      r3, [r0, #-8]
            b        1b
        3:
            mrs      r2, psp
            stmdb    r2!, {r3, r4-r11}
            ldr      r3, [sp]
            str      r2, [r3]
            b        0b
        "   :
            : "{r0}"(stack_ptr), "{r1}"(data_ptr)
            : "cc", "memory"
            : "volatile"
        );
        unreachable();
    }
}

impl SvService for SwitchBackService {
    #[allow(clippy::too_many_lines)]
    unsafe extern "C" fn handler(&mut self) {
        #[cfg(feature = "std")]
        return unimplemented!();
        let Self {
            data_ptr,
            data_size,
        } = *self;
        #[cfg(all(
            feature = "fpu",
            any(cortex_m_core = "cortex_m4f_r0p0", cortex_m_core = "cortex_m4f_r0p1")
        ))]
        asm!("
            movw     r2, #0xED94
            movt     r2, #0xE000
            mov      r3, #0
            str      r3, [r2]
            mrs      r3, control
            mrs      r12, psp
            tst      lr, #0x10
            it       eq
            vstmdbeq r12!, {s16-s31}
            stmdb    r12!, {r3, r4-r11, lr}
            pop      {r2, r3, r4, lr}
            stmdb    r12!, {r4}
            str      r12, [r2]
            ldr      r2, [r0]
            cmp      r2, r3
            beq      2f
            str      r3, [r0]
            and      r12, r1, #3
            subs     r1, r1, r12
            beq      1f
        0:
            ldr      r0, [r2], #4
            str      r0, [r3], #4
            subs     r1, r1, #4
            bne      0b
        1:
            lsrs     r12, r12, #1
            itt      ne
            ldrhne   r0, [r2], #2
            strhne   r0, [r3], #2
            itt      cs
            ldrbcs   r0, [r2], #1
            strbcs   r0, [r3], #1
        2:
            tst      lr, #0x4
            bne      3f
            ldmia    sp!, {r3, r4-r11}
            tst      lr, #0x10
            it       eq
            vldmiaeq sp!, {s16-s31}
            msr      control, r3
            bx       lr
        3:
            ldr      r0, [sp]
            ldr      r0, [r0]
            ldmia    r0!, {r3, r4-r11}
            tst      lr, #0x10
            it       eq
            vldmiaeq r0!, {s16-s31}
            msr      psp, r0
            msr      control, r3
            bx       lr
        "   :
            : "{r0}"(data_ptr), "{r1}"(data_size)
            : "cc", "memory"
            : "volatile"
        );
        #[cfg(not(all(
            feature = "fpu",
            any(cortex_m_core = "cortex_m4f_r0p0", cortex_m_core = "cortex_m4f_r0p1")
        )))]
        asm!("
            movw     r2, #0xED94
            movt     r2, #0xE000
            mov      r3, #0
            str      r3, [r2]
            mrs      r3, control
            mrs      r12, psp
            stmdb    r12!, {r3, r4-r11, lr}
            pop      {r2, r3, r4, lr}
            stmdb    r12!, {r4}
            str      r12, [r2]
            ldr      r2, [r0]
            cmp      r2, r3
            beq      2f
            str      r3, [r0]
            and      r12, r1, #3
            subs     r1, r1, r12
            beq      1f
        0:
            ldr      r0, [r2], #4
            str      r0, [r3], #4
            subs     r1, r1, #4
            bne      0b
        1:
            lsrs     r12, r12, #1
            itt      ne
            ldrhne   r0, [r2], #2
            strhne   r0, [r3], #2
            itt      cs
            ldrbcs   r0, [r2], #1
            strbcs   r0, [r3], #1
        2:
            tst      lr, #0x4
            ittt     eq
            ldmiaeq  sp!, {r3, r4-r11}
            msreq    control, r3
            bxeq     lr
            ldr      r0, [sp]
            ldr      r0, [r0]
            ldmia    r0!, {r3, r4-r11}
            msr      psp, r0
            msr      control, r3
            bx       lr
        "   :
            : "{r0}"(data_ptr), "{r1}"(data_size)
            : "cc", "memory"
            : "volatile"
        );
        unreachable();
    }
}

impl<Sv, T> Switch<T> for Sv
where
    Sv: SvCall<SwitchContextService>,
    Sv: SvCall<SwitchBackService>,
{
    unsafe fn switch_context(data: *mut T, stack_ptr: *mut *const u8) {
        Self::call(&mut SwitchContextService {
            stack_ptr,
            data_ptr: data as *mut u8,
        });
    }

    unsafe fn switch_back(data: *mut *mut T) {
        Self::call(&mut SwitchBackService {
            data_ptr: data as *mut *mut u8,
            data_size: size_of::<T>(),
        });
    }
}