-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcontrol.rs
More file actions
196 lines (170 loc) · 5.64 KB
/
Copy pathcontrol.rs
File metadata and controls
196 lines (170 loc) · 5.64 KB
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
use core::{convert::TryInto, result::Result::Ok};
use crate::{
arch::arm64::fptest::rros_detect_fpu,
factory::{CloneData, RrosFactory, RrosFactoryInside},
};
use kernel::{
bindings,
device::DeviceType,
file::File,
file_operations::{FileOpener, FileOperations, IoctlCommand},
io_buffer::{IoBufferWriter, WritableToBytes},
memory_rros::{RROS_SHARED_HEAP, RROS_SHM_SIZE},
mm::{remap_pfn_range, PAGE_SHARED},
prelude::*,
str::CStr,
sync::SpinLock,
new_spinlock,
};
pub const CONFIG_RROS_NR_CONTROL: usize = 0;
pub static mut RROS_CONTROL_FACTORY: Pin<Box<SpinLock<RrosFactory>>> = unsafe {
Box::pin_init(
new_spinlock!(
RrosFactory {
name: CStr::from_bytes_with_nul_unchecked("control\0".as_bytes()),
nrdev: CONFIG_RROS_NR_CONTROL,
build: None,
dispose: None,
attrs: None,
flags: crate::factory::RrosFactoryType::SINGLE,
inside: Some(RrosFactoryInside {
type_: DeviceType::new(),
class: None,
cdev: None,
device: None,
sub_rdev: None,
kuid: None,
kgid: None,
minor_map: None,
index: None,
name_hash: None,
hash_lock: None,
register: None,
}),
}
)
).unwrap()
};
pub struct ControlOps;
impl FileOpener<u8> for ControlOps {
fn open(shared: &u8, _fileref: &File) -> Result<Self::Wrapper> {
// there should be some checks
let mut data = CloneData::default();
data.ptr = shared as *const u8 as *mut u8;
pr_debug!("open control device success");
Ok(Box::try_new(data)?)
}
}
impl FileOperations for ControlOps {
kernel::declare_file_operations!(oob_ioctl, ioctl, mmap);
type Wrapper = Box<CloneData>;
// fn ioctl
fn ioctl(_this: &CloneData, file: &File, cmd: &mut IoctlCommand) -> Result<i32> {
pr_debug!("I'm the ioctl ops of the control factory");
// cmd.dispatch::<Self>(this, file)
let ret = control_ioctl(file, cmd);
pr_debug!("the value of ret is {}", ret.unwrap());
ret
}
// fn oob_ioctl
fn oob_ioctl(_this: &CloneData, file: &File, cmd: &mut IoctlCommand) -> Result<i32> {
pr_debug!("I'm the ioctl ops of the control factory");
let ret = control_common_ioctl(file, cmd);
ret
}
fn mmap(_this: &CloneData, file: &File, vma: &mut bindings::vm_area_struct) -> Result {
pr_debug!("I'm the mmap ops of the control factory");
let ret = control_mmap(file, vma);
ret
}
}
#[repr(C)]
pub struct RrosCoreInfo {
abi_base: u32,
abi_current: u32,
fpu_features: u32,
shm_size: u64,
}
impl RrosCoreInfo {
pub fn new() -> Self {
RrosCoreInfo {
abi_base: 0,
abi_current: 0,
fpu_features: 0,
shm_size: 0,
}
}
}
unsafe impl WritableToBytes for RrosCoreInfo {}
#[repr(C)]
#[allow(dead_code)]
pub struct RrosCpuState {
cpu: u32,
state_ptr: u64,
}
// pub const RROS_CONTROL_IOCBASE: u32 = 'C';
pub const RROS_ABI_BASE: u32 = 23;
pub const RROS_ABI_LEVEL: u32 = 26;
pub const RROS_CTLIOC_GET_COREINFO: u32 = 2149073664;
// pub const RROS_CTLIOC_SCHEDCTL: u32 = 3222815489;
// pub const RROS_CTLIOC_GET_CPUSTATE: u32 = 2148549378;
extern "C" {
fn rust_helper_pa(x: usize) -> usize;
}
fn control_ioctl(file: &File, cmd: &mut IoctlCommand) -> Result<i32> {
let mut info = RrosCoreInfo::new();
match cmd.cmd {
RROS_CTLIOC_GET_COREINFO => {
info.abi_base = RROS_ABI_BASE;
info.abi_current = RROS_ABI_LEVEL;
// FIXME: Currently, FPU support is only implemented for the ARM64 architecture.
// When RROS is adapted to other architectures, FPU support for those architectures
// will need to be enabled as well.
info.fpu_features = rros_detect_fpu();
unsafe {
pr_debug!(
"the value of info.shm_size and RROS_SHM_SIZE is {}, {}",
info.shm_size,
RROS_SHM_SIZE
)
};
unsafe {
info.shm_size = RROS_SHM_SIZE as u64;
}
unsafe {
pr_debug!(
"the value of info.shm_size and RROS_SHM_SIZE is {}, {}",
info.shm_size,
RROS_SHM_SIZE
)
};
// ret = cmd.user_slice.take().ok_or(Error::EINVAL).writer();
let data = cmd.user_slice.take().ok_or(Error::EINVAL);
data.unwrap().writer().write(&info)?;
Ok(0)
}
_ => control_common_ioctl(file, cmd),
}
}
fn control_common_ioctl(_file: &File, _cmd: &mut IoctlCommand) -> Result<i32> {
Ok(0)
}
fn control_mmap(_file: &File, vma: &mut bindings::vm_area_struct) -> Result {
let p = unsafe { RROS_SHARED_HEAP.membase };
let pfn: usize = unsafe { rust_helper_pa(p as usize) } >> bindings::PAGE_SHIFT;
let len: usize = unsafe {
(vma.__bindgen_anon_1.__bindgen_anon_1.vm_end
- vma.__bindgen_anon_1.__bindgen_anon_1.vm_start) as usize
};
if len != unsafe { RROS_SHM_SIZE } {
return Err(Error::EINVAL);
}
remap_pfn_range(
vma as *mut bindings::vm_area_struct,
unsafe { vma.__bindgen_anon_1.__bindgen_anon_1.vm_start },
pfn.try_into().unwrap(),
len.try_into().unwrap(),
PAGE_SHARED,
);
Ok(())
}