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
use core::nonzero::NonZero;
use memop;
use platform::{Chip, Platform};
use platform::mpu::MPU;
use platform::systick::SysTick;
use process;
use process::{Process, Task};
use returncode::ReturnCode;
use syscall::Syscall;
const KERNEL_TICK_DURATION_US: u32 = 10000;
const MIN_QUANTA_THRESHOLD_US: u32 = 500;
pub unsafe fn do_process<P: Platform, C: Chip>(
platform: &P,
chip: &mut C,
process: &mut Process,
appid: ::AppId,
ipc: &::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.value() <= 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.schedule_callback(appid, otherapp, ipc_type);
}
}
continue;
}
},
process::State::Fault => {
panic!("Attempted to schedule a faulty process");
}
}
if !process.syscall_fired() {
break;
}
if process.app_fault() {
process.fault_state();
continue;
}
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();
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 res = if callback_ptr_raw as usize == 0 {
ReturnCode::EINVAL
} else {
let callback_ptr = NonZero::new_unchecked(callback_ptr_raw);
let callback = ::Callback::new(appid, appdata, callback_ptr);
platform.with_driver(driver_num, |driver| match driver {
Some(d) => d.subscribe(subdriver_num, callback),
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;
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(), slice)
} else {
ReturnCode::EINVAL
}
}
None => ReturnCode::ENODEVICE,
}
});
process.set_return_code(res);
}
_ => {}
}
}
systick.reset();
}