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
//! Tock core scheduler.

use core::ptr;
use core::ptr::NonNull;

use callback;
use callback::{AppId, Callback};
use ipc;
use mem::AppSlice;
use memop;
use platform::mpu::MPU;
use platform::systick::SysTick;
use platform::{Chip, Platform};
use process;
use process::{Process, Task};
use returncode::ReturnCode;
use syscall::Syscall;

/// The time a process is permitted to run before being pre-empted
const KERNEL_TICK_DURATION_US: u32 = 10000;
/// Skip re-scheduling a process if its quanta is nearly exhausted
const MIN_QUANTA_THRESHOLD_US: u32 = 500;

/// Main loop.
pub fn kernel_loop<P: Platform, C: Chip>(
    platform: &P,
    chip: &mut C,
    processes: &'static mut [Option<&mut process::Process<'static>>],
    ipc: Option<&ipc::IPC>,
) {
    let processes = unsafe {
        process::PROCS = processes;
        &mut process::PROCS
    };

    loop {
        unsafe {
            chip.service_pending_interrupts();

            for (i, p) in processes.iter_mut().enumerate() {
                p.as_mut().map(|process| {
                    do_process(platform, chip, process, callback::AppId::new(i), ipc);
                });
                if chip.has_pending_interrupts() {
                    break;
                }
            }

            chip.atomic(|| {
                if !chip.has_pending_interrupts() && process::processes_blocked() {
                    chip.sleep();
                }
            });
        };
    }
}

unsafe fn do_process<P: Platform, C: Chip>(
    platform: &P,
    chip: &mut C,
    process: &mut Process,
    appid: AppId,
    ipc: Option<&::ipc::IPC>,
) {
    let systick = chip.systick();
    systick.reset();
    systick.set_timer(KERNEL_TICK_DURATION_US);
    systick.enable(true);

    loop {
        if chip.has_pending_interrupts()
            || systick.overflowed()
            || !systick.greater_than(MIN_QUANTA_THRESHOLD_US)
        {
            break;
        }

        match process.current_state() {
            process::State::Running => {
                process.setup_mpu(chip.mpu());
                chip.mpu().enable_mpu();
                systick.enable(true);
                process.switch_to();
                systick.enable(false);
                chip.mpu().disable_mpu();
            }
            process::State::Yielded => match process.dequeue_task() {
                None => break,
                Some(cb) => {
                    match cb {
                        Task::FunctionCall(ccb) => {
                            process.push_function_call(ccb);
                        }
                        Task::IPC((otherapp, ipc_type)) => {
                            ipc.map_or_else(
                                || {
                                    assert!(
                                        false,
                                        "Kernel consistency error: IPC Task with no IPC"
                                    );
                                },
                                |ipc| {
                                    ipc.schedule_callback(appid, otherapp, ipc_type);
                                },
                            );
                        }
                    }
                    continue;
                }
            },
            process::State::Fault => {
                // we should never be scheduling a process in fault
                panic!("Attempted to schedule a faulty process");
            }
        }

        if !process.syscall_fired() {
            break;
        }

        // check if the app had a fault
        if process.app_fault() {
            // let process deal with it as appropriate
            process.fault_state();
            continue;
        }

        // process had a system call, count it
        process.incr_syscall_count();
        match process.svc_number() {
            Some(Syscall::MEMOP) => {
                let res = memop::memop(process);
                process.set_return_code(res);
            }
            Some(Syscall::YIELD) => {
                process.yield_state();
                process.pop_syscall_stack();

                // There might be already enqueued callbacks
                continue;
            }
            Some(Syscall::SUBSCRIBE) => {
                let driver_num = process.r0();
                let subdriver_num = process.r1();
                let callback_ptr_raw = process.r2() as *mut ();
                let appdata = process.r3();

                let callback_ptr = NonNull::new(callback_ptr_raw);
                let callback = callback_ptr.map(|ptr| Callback::new(appid, appdata, ptr.cast()));

                let res = platform.with_driver(driver_num, |driver| match driver {
                    Some(d) => d.subscribe(subdriver_num, callback, appid),
                    None => ReturnCode::ENODEVICE,
                });
                process.set_return_code(res);
            }
            Some(Syscall::COMMAND) => {
                let res = platform.with_driver(process.r0(), |driver| match driver {
                    Some(d) => d.command(process.r1(), process.r2(), process.r3(), appid),
                    None => ReturnCode::ENODEVICE,
                });
                process.set_return_code(res);
            }
            Some(Syscall::ALLOW) => {
                let res = platform.with_driver(process.r0(), |driver| {
                    match driver {
                        Some(d) => {
                            let start_addr = process.r2() as *mut u8;
                            if start_addr != ptr::null_mut() {
                                let size = process.r3();
                                if process.in_exposed_bounds(start_addr, size) {
                                    let slice = AppSlice::new(start_addr as *mut u8, size, appid);
                                    d.allow(appid, process.r1(), Some(slice))
                                } else {
                                    ReturnCode::EINVAL /* memory not allocated to process */
                                }
                            } else {
                                d.allow(appid, process.r1(), None)
                            }
                        }
                        None => ReturnCode::ENODEVICE,
                    }
                });
                process.set_return_code(res);
            }
            _ => {}
        }
    }
    systick.reset();
}