kernel/grant.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Support for processes granting memory from their allocations to the kernel.
6//!
7//! ## Grant Overview
8//!
9//! Grants allow capsules to dynamically allocate memory from a process to hold
10//! state on the process's behalf.
11//!
12//! Each capsule that wishes to do this needs to have a [`Grant`] type. Grants
13//! are created at boot, and each have a unique ID and a type `T`. This type
14//! only allows the capsule to allocate memory from a process in the future. It
15//! does not initially represent any allocated memory.
16//!
17//! When a capsule does wish to use its Grant to allocate memory from a process,
18//! it must "enter" the Grant with a specific [`ProcessId`]. Entering a Grant
19//! for a specific process instructs the core kernel to create an object `T` in
20//! the process's memory space and provide the capsule with access to it. If the
21//! Grant has not previously been entered for that process, the memory for
22//! object `T` will be allocated from the "grant region" within the
23//! kernel-accessible portion of the process's memory.
24//!
25//! If a Grant has never been entered for a process, the object `T` will _not_
26//! be allocated in that process's grant region, even if the `Grant` has been
27//! entered for other processes.
28//!
29//! Upcalls and allowed buffer references are stored in the dynamically
30//! allocated grant for a particular Driver as well. Upcalls and allowed buffer
31//! references are stored outside of the `T` object to enable the kernel to
32//! manage them and ensure swapping guarantees are met.
33//!
34//! The type `T` of a Grant is fixed in size and the number of upcalls and
35//! allowed buffers associated with a grant is fixed. That is, when a Grant is
36//! entered for a process the resulting allocated object will be the size of
37//! `SizeOf<T>` plus the size for the structure to hold upcalls and allowed
38//! buffer references. If capsules need additional process-specific memory for
39//! their operation, they can use an [`GrantRegionAllocator`] to request
40//! additional memory from the process's grant region.
41//!
42//! ```text,ignore
43//! ┌──────────────────┐
44//! │ │
45//! │ Capsule │
46//! │ │
47//! └─┬────────────────┘
48//! │ Capsules hold
49//! │ references to
50//! │ grants.
51//! ▼
52//! ┌──────────────────┐
53//! │ Grant │
54//! │ │
55//! Process Memory │ Type: T │
56//! ┌────────────────────────┐ │ grant_num: 1 │
57//! │ │ │ driver_num: 0x4 │
58//! │ ... │ └───┬─────────────┬┘
59//! ├────────────────────────┤ │Each Grant │
60//! │ Grant ptr 0 │ │has a pointer│
61//! │ Pointers ptr 1 ───┐ │ ◄───┘per process. │
62//! │ ... │ │ │
63//! │ ptr N │ │ │
64//! ├──────────────────────┼─┤ │
65//! │ ... │ │ │
66//! ├──────────────────────┼─┤ │
67//! │ Grant Region │ │ When a Grant │
68//! │ │ │ is allocated │
69//! │ ┌─────────────────┐ │ │ for a process │
70//! │ │ Allocated Grant │ │ │ ◄─────────────────┘
71//! │ │ │ │ │ it uses memory
72//! │ │ [ SizeOf<T> ] │ │ │ from the grant
73//! │ │─────────────────│ │ │ region.
74//! │ │ Padding │ │ │
75//! │ │─────────────────│ │ │
76//! │ │ GrantKernelData │ │ │
77//! │ └─────────────────┘◄─┘ │
78//! │ │
79//! │ ┌─────────────────┐ │
80//! │ │ Custom Grant │ │ ◄── Capsules can
81//! │ │ │ │ allocate extra
82//! │ └─────────────────┘ │ memory if needed.
83//! │ │
84//! ├─kernel_brk─────────────┤
85//! │ │
86//! │ ... │
87//! └────────────────────────┘
88//! ```
89//!
90//! ## Grant Mechanisms and Types
91//!
92//! Here is an overview of the types used by grant.rs to implement the Grant
93//! functionality in Tock:
94//!
95//! ```text,ignore
96//! ┌──────────────────────────┐
97//! │ struct Grant<T, ...> { │
98//! │ driver_num: usize │
99//! │ grant_num: usize │
100//! │ } ├───┐
101//! Entering a Grant for a └──┬───────────────────────┘ │
102//! process causes the │ │
103//! memory for T to be │ .enter(ProcessId) │ .enter(ProcessId, fn)
104//! allocated. ▼ │
105//! ┌──────────────────────────┐ │ For convenience,
106//! ProcessGrant represents │ struct ProcessGrant<T> { │ │ allocating and getting
107//! a Grant allocated for a │ number: usize │ │ access to the T object
108//! specific process. │ process: &Process │ │ is combined in one
109//! │ } │ │ .enter() call.
110//! A provided closure └──┬───────────────────────┘ │
111//! is given access to │ │
112//! the underlying memory │ .enter(fn) │
113//! where the T is stored. ▼ │
114//! ┌────────────────────────────┐ │
115//! GrantData wraps the │ struct GrantData<T> { │◄┘
116//! type and provides │ data: &mut T │
117//! mutable access. │ } │
118//! GrantKernelData │ struct GrantKernelData { │
119//! provides access to │ upcalls: [SavedUpcall] │
120//! scheduling upcalls │ allow_ro: [SavedAllowRo] │
121//! and process buffers. │ allow_rw: [SavedAllowRW] │
122//! │ } │
123//! └──┬─────────────────────────┘
124//! The actual object T can │
125//! only be accessed inside │ fn(mem: &GrantData, kernel_data: &GrantKernelData)
126//! the closure. ▼
127//! ```
128
129use core::cmp;
130use core::marker::PhantomData;
131use core::mem::{align_of, size_of};
132use core::ops::{Deref, DerefMut};
133use core::ptr::{write, NonNull};
134use core::slice;
135
136use crate::config;
137use crate::debug;
138use crate::kernel::Kernel;
139use crate::process::ProcessSlot;
140use crate::process::{Error, Process, ProcessCustomGrantIdentifier, ProcessId};
141use crate::processbuffer::{ReadOnlyProcessBuffer, ReadWriteProcessBuffer};
142use crate::processbuffer::{ReadOnlyProcessBufferRef, ReadWriteProcessBufferRef};
143use crate::upcall::{Upcall, UpcallError, UpcallId};
144use crate::utilities::capability_ptr::CapabilityPtr;
145use crate::utilities::machine_register::MachineRegister;
146use crate::ErrorCode;
147
148/// Tracks how many upcalls a grant instance supports automatically.
149pub trait UpcallSize {
150 /// The number of upcalls the grant supports.
151 const COUNT: u8;
152}
153
154/// Specifies how many upcalls a grant instance supports automatically.
155pub struct UpcallCount<const NUM: u8>;
156impl<const NUM: u8> UpcallSize for UpcallCount<NUM> {
157 const COUNT: u8 = NUM;
158}
159
160/// Tracks how many read-only allows a grant instance supports automatically.
161pub trait AllowRoSize {
162 /// The number of read-only allows the grant supports.
163 const COUNT: u8;
164}
165
166/// Specifies how many read-only allows a grant instance supports automatically.
167pub struct AllowRoCount<const NUM: u8>;
168impl<const NUM: u8> AllowRoSize for AllowRoCount<NUM> {
169 const COUNT: u8 = NUM;
170}
171
172/// Tracks how many read-write allows a grant instance supports automatically.
173pub trait AllowRwSize {
174 /// The number of read-write allows the grant supports.
175 const COUNT: u8;
176}
177
178/// Specifies how many read-write allows a grant instance supports
179/// automatically.
180pub struct AllowRwCount<const NUM: u8>;
181impl<const NUM: u8> AllowRwSize for AllowRwCount<NUM> {
182 const COUNT: u8 = NUM;
183}
184
185/// Helper that calculated offsets within the kernel owned memory (i.e. the
186/// non-T part of grant).
187///
188/// Example layout of full grant belonging to a single app and driver:
189///
190/// ```text,ignore
191/// 0x003FFC8 ┌────────────────────────────────────┐
192/// │ T |
193/// 0x003FFxx ├ ───────────────────────── ┐ K |
194/// │ Padding (ensure T aligns)| e |
195/// 0x003FF44 ├ ───────────────────────── | r |
196/// │ SavedAllowRwN | n |
197/// │ ... | e | G
198/// │ SavedAllowRw1 | l | r
199/// │ SavedAllowRw0 | | a
200/// 0x003FF44 ├ ───────────────────────── | O | n
201/// │ SavedAllowRoN | w | t
202/// │ ... | n |
203/// │ SavedAllowRo1 | e | M
204/// │ SavedAllowRo0 | d | e
205/// 0x003FF30 ├ ───────────────────────── | | m
206/// │ SavedUpcallN | D | o
207/// │ ... | a | r
208/// │ SavedUpcall1 | t | y
209/// │ SavedUpcall0 | a |
210/// 0x003FF24 ├ ───────────────────────── | |
211/// │ Counters (usize) | |
212/// 0x003FF20 └────────────────────────────────────┘
213/// ```
214///
215/// The counters structure is composed as:
216///
217/// ```text,ignore
218/// 0 1 2 3 bytes
219/// |-------------|-------------|-------------|-------------|
220/// | # Upcalls | # RO Allows | # RW Allows | [unused] |
221/// |-------------|-------------|-------------|-------------|
222/// ```
223///
224/// This type is created whenever a grant is entered, and is responsible for
225/// ensuring that the grant is closed when it is no longer used. On `Drop`, we
226/// leave the grant. This protects against calling `grant.enter()` without
227/// calling the corresponding `grant.leave()`, perhaps due to accidentally using
228/// the `?` operator.
229struct EnteredGrantKernelManagedLayout<'a> {
230 /// Leaving a grant is handled through the process implementation, so must
231 /// keep a reference to the relevant process.
232 process: &'a dyn Process,
233 /// The grant number of the entered grant that we want to ensure we leave
234 /// properly.
235 grant_num: usize,
236
237 /// The location of the counters structure for the grant.
238 counters_ptr: *mut usize,
239 /// Pointer to the array of saved upcalls.
240 upcalls_array: *mut SavedUpcall,
241 /// Pointer to the array of saved read-only allows.
242 allow_ro_array: *mut SavedAllowRo,
243 /// Pointer to the array of saved read-write allows.
244 allow_rw_array: *mut SavedAllowRw,
245}
246
247/// Represents the number of the upcall elements in the kernel owned section of
248/// the grant.
249#[derive(Copy, Clone)]
250struct UpcallItems(u8);
251/// Represents the number of the read-only allow elements in the kernel owned
252/// section of the grant.
253#[derive(Copy, Clone)]
254struct AllowRoItems(u8);
255/// Represents the number of the read-write allow elements in the kernel owned
256/// section of the grant.
257#[derive(Copy, Clone)]
258struct AllowRwItems(u8);
259/// Represents the size data (in bytes) T within the grant.
260#[derive(Copy, Clone)]
261struct GrantDataSize(usize);
262/// Represents the alignment of data T within the grant.
263#[derive(Copy, Clone)]
264struct GrantDataAlign(usize);
265
266impl<'a> EnteredGrantKernelManagedLayout<'a> {
267 /// Reads the specified pointer as the base of the kernel owned grant region
268 /// that has previously been initialized.
269 ///
270 /// # Safety
271 ///
272 /// The incoming base pointer must be well aligned and already contain
273 /// initialized data in the expected form. There must not be any other
274 /// `EnteredGrantKernelManagedLayout` for the given `base_ptr` at the same
275 /// time, otherwise multiple mutable references to the same upcall/allow
276 /// slices could be created.
277 unsafe fn read_from_base(
278 base_ptr: NonNull<u8>,
279 process: &'a dyn Process,
280 grant_num: usize,
281 ) -> Self {
282 let counters_ptr = base_ptr.as_ptr() as *mut usize;
283 let counters_val = counters_ptr.read();
284
285 // Parse the counters field for each of the fields
286 let [_, _, allow_ro_num, upcalls_num] = u32::to_be_bytes(counters_val as u32);
287
288 // Skip over the counter usize, then the stored array of `SavedAllowRo`
289 // items and `SavedAllowRw` items.
290 let upcalls_array = counters_ptr.add(1) as *mut SavedUpcall;
291 let allow_ro_array = upcalls_array.add(upcalls_num as usize) as *mut SavedAllowRo;
292 let allow_rw_array = allow_ro_array.add(allow_ro_num as usize) as *mut SavedAllowRw;
293
294 Self {
295 process,
296 grant_num,
297 counters_ptr,
298 upcalls_array,
299 allow_ro_array,
300 allow_rw_array,
301 }
302 }
303
304 /// Creates a layout from the specified pointer and lengths of arrays and
305 /// initializes the kernel owned portion of the layout.
306 ///
307 /// # Safety
308 ///
309 /// The incoming base pointer must be well aligned and reference enough
310 /// memory to hold the entire kernel managed grant structure. There must
311 /// not be any other `EnteredGrantKernelManagedLayout` for
312 /// the given `base_ptr` at the same time, otherwise multiple mutable
313 /// references to the same upcall/allow slices could be created.
314 unsafe fn initialize_from_counts(
315 base_ptr: NonNull<u8>,
316 upcalls_num_val: UpcallItems,
317 allow_ro_num_val: AllowRoItems,
318 allow_rw_num_val: AllowRwItems,
319 process: &'a dyn Process,
320 grant_num: usize,
321 ) -> Self {
322 let counters_ptr = base_ptr.as_ptr() as *mut usize;
323
324 // Create the counters usize value by correctly packing the various
325 // counts into 8 bit fields.
326 let counter: usize =
327 u32::from_be_bytes([0, allow_rw_num_val.0, allow_ro_num_val.0, upcalls_num_val.0])
328 as usize;
329
330 let upcalls_array = counters_ptr.add(1) as *mut SavedUpcall;
331 let allow_ro_array = upcalls_array.add(upcalls_num_val.0.into()) as *mut SavedAllowRo;
332 let allow_rw_array = allow_ro_array.add(allow_ro_num_val.0.into()) as *mut SavedAllowRw;
333
334 counters_ptr.write(counter);
335 write_default_array(upcalls_array, upcalls_num_val.0.into());
336 write_default_array(allow_ro_array, allow_ro_num_val.0.into());
337 write_default_array(allow_rw_array, allow_rw_num_val.0.into());
338
339 Self {
340 process,
341 grant_num,
342 counters_ptr,
343 upcalls_array,
344 allow_ro_array,
345 allow_rw_array,
346 }
347 }
348
349 /// Returns the entire grant size including the kernel owned memory,
350 /// padding, and data for T. Requires that grant_t_align be a power of 2,
351 /// which is guaranteed from align_of rust calls.
352 fn grant_size(
353 upcalls_num: UpcallItems,
354 allow_ro_num: AllowRoItems,
355 allow_rw_num: AllowRwItems,
356 grant_t_size: GrantDataSize,
357 grant_t_align: GrantDataAlign,
358 ) -> usize {
359 let kernel_managed_size = size_of::<usize>()
360 + upcalls_num.0 as usize * size_of::<SavedUpcall>()
361 + allow_ro_num.0 as usize * size_of::<SavedAllowRo>()
362 + allow_rw_num.0 as usize * size_of::<SavedAllowRw>();
363 // We know that grant_t_align is a power of 2, so we can make a mask
364 // that will save only the remainder bits.
365 let grant_t_align_mask = grant_t_align.0 - 1;
366 // Determine padding to get to the next multiple of grant_t_align by
367 // taking the remainder and subtracting that from the alignment, then
368 // ensuring a full alignment value maps to 0.
369 let padding =
370 (grant_t_align.0 - (kernel_managed_size & grant_t_align_mask)) & grant_t_align_mask;
371 kernel_managed_size + padding + grant_t_size.0
372 }
373
374 /// Returns the alignment of the entire grant region based on the alignment
375 /// of data T.
376 fn grant_align(grant_t_align: GrantDataAlign) -> usize {
377 // The kernel owned memory all aligned to usize. We need to use the
378 // higher of the two alignment to ensure our padding calculations work
379 // for any alignment of T.
380 cmp::max(align_of::<usize>(), grant_t_align.0)
381 }
382
383 /// Returns the offset for the grant data t within the entire grant region.
384 ///
385 /// # Safety
386 ///
387 /// The caller must ensure that the specified base pointer is aligned to at
388 /// least the alignment of T and points to a grant that is of size
389 /// grant_size bytes.
390 unsafe fn offset_of_grant_data_t(
391 base_ptr: NonNull<u8>,
392 grant_size: usize,
393 grant_t_size: GrantDataSize,
394 ) -> NonNull<u8> {
395 // The location of the grant data T is the last element in the entire
396 // grant region. Caller must verify that memory is accessible and well
397 // aligned to T.
398 let grant_t_size_usize: usize = grant_t_size.0;
399 NonNull::new_unchecked(base_ptr.as_ptr().add(grant_size - grant_t_size_usize))
400 }
401
402 /// Read an 8 bit value from the counter field offset by the specified
403 /// number of bits. This is a helper function for reading the counter field.
404 fn get_counter_offset(&self, offset_bits: usize) -> usize {
405 // # Safety
406 //
407 // Creating a `EnteredGrantKernelManagedLayout` object requires that the
408 // pointers are well aligned and point to valid memory.
409 let counters_val = unsafe { self.counters_ptr.read() };
410 (counters_val >> offset_bits) & 0xFF
411 }
412
413 /// Return the number of upcalls stored by the core kernel for this grant.
414 fn get_upcalls_number(&self) -> usize {
415 self.get_counter_offset(0)
416 }
417
418 /// Return the number of read-only allow buffers stored by the core kernel
419 /// for this grant.
420 fn get_allow_ro_number(&self) -> usize {
421 self.get_counter_offset(8)
422 }
423
424 /// Return the number of read-write allow buffers stored by the core kernel
425 /// for this grant.
426 fn get_allow_rw_number(&self) -> usize {
427 self.get_counter_offset(16)
428 }
429
430 /// Return mutable access to the slice of stored upcalls for this grant.
431 /// This is necessary for storing a new upcall.
432 fn get_upcalls_slice(&mut self) -> &mut [SavedUpcall] {
433 // # Safety
434 //
435 // Creating a `EnteredGrantKernelManagedLayout` object ensures that the
436 // pointer to the upcall array is valid.
437 unsafe { slice::from_raw_parts_mut(self.upcalls_array, self.get_upcalls_number()) }
438 }
439
440 /// Return mutable access to the slice of stored read-only allow buffers for
441 /// this grant. This is necessary for storing a new read-only allow.
442 fn get_allow_ro_slice(&mut self) -> &mut [SavedAllowRo] {
443 // # Safety
444 //
445 // Creating a `EnteredGrantKernelManagedLayout` object ensures that the
446 // pointer to the RO allow array is valid.
447 unsafe { slice::from_raw_parts_mut(self.allow_ro_array, self.get_allow_ro_number()) }
448 }
449
450 /// Return mutable access to the slice of stored read-write allow buffers
451 /// for this grant. This is necessary for storing a new read-write allow.
452 fn get_allow_rw_slice(&mut self) -> &mut [SavedAllowRw] {
453 // # Safety
454 //
455 // Creating a `EnteredGrantKernelManagedLayout` object ensures that the
456 // pointer to the RW allow array is valid.
457 unsafe { slice::from_raw_parts_mut(self.allow_rw_array, self.get_allow_rw_number()) }
458 }
459
460 /// Return slices to the kernel managed upcalls and allow buffers. This
461 /// permits using upcalls and allow buffers when a capsule is accessing a
462 /// grant.
463 fn get_resource_slices(&self) -> (&[SavedUpcall], &[SavedAllowRo], &[SavedAllowRw]) {
464 // # Safety
465 //
466 // Creating a `EnteredGrantKernelManagedLayout` object ensures that the
467 // pointer to the upcall array is valid.
468 let upcall_slice =
469 unsafe { slice::from_raw_parts(self.upcalls_array, self.get_upcalls_number()) };
470
471 // # Safety
472 //
473 // Creating a `EnteredGrantKernelManagedLayout` object ensures that the
474 // pointer to the RO allow array is valid.
475 let allow_ro_slice =
476 unsafe { slice::from_raw_parts(self.allow_ro_array, self.get_allow_ro_number()) };
477
478 // # Safety
479 //
480 // Creating a `KernelManagedLayout` object ensures that the pointer to
481 // the RW allow array is valid.
482 let allow_rw_slice =
483 unsafe { slice::from_raw_parts(self.allow_rw_array, self.get_allow_rw_number()) };
484
485 (upcall_slice, allow_ro_slice, allow_rw_slice)
486 }
487}
488
489// Ensure that we leave the grant once this goes out of scope.
490impl Drop for EnteredGrantKernelManagedLayout<'_> {
491 fn drop(&mut self) {
492 // ### Safety
493 //
494 // To safely call this function we must ensure that no references will
495 // exist to the grant once `leave_grant()` returns. Because using a
496 // `EnteredGrantKernelManagedLayout` object is the only only way we
497 // access the actual memory of a grant, and we are calling
498 // `leave_grant()` from its `drop()` method, we are sure there will be
499 // no remaining references to the grant.
500 unsafe {
501 self.process.leave_grant(self.grant_num);
502 }
503 }
504}
505
506/// This [`GrantData`] object provides access to the memory allocated for a
507/// grant for a specific process.
508///
509/// The [`GrantData`] type is templated on `T`, the actual type of the object in
510/// the grant. [`GrantData'] holds a mutable reference to the type, allowing
511/// users access to the object in process memory.
512///
513/// Capsules gain access to a [`GrantData`] object by calling
514/// [`Grant::enter()`].
515pub struct GrantData<'a, T: 'a + ?Sized> {
516 /// The mutable reference to the actual object type stored in the grant.
517 data: &'a mut T,
518}
519
520impl<'a, T: 'a + ?Sized> GrantData<'a, T> {
521 /// Create a [`GrantData`] object to provide access to the actual object
522 /// allocated for a process.
523 ///
524 /// Only one can [`GrantData`] per underlying object can be created at a
525 /// time. Otherwise, there would be multiple mutable references to the same
526 /// object which is undefined behavior.
527 fn new(data: &'a mut T) -> GrantData<'a, T> {
528 GrantData { data }
529 }
530}
531
532impl<'a, T: 'a + ?Sized> Deref for GrantData<'a, T> {
533 type Target = T;
534 fn deref(&self) -> &T {
535 self.data
536 }
537}
538
539impl<'a, T: 'a + ?Sized> DerefMut for GrantData<'a, T> {
540 fn deref_mut(&mut self) -> &mut T {
541 self.data
542 }
543}
544
545/// This [`GrantKernelData`] object provides a handle to access upcalls and
546/// process buffers stored on behalf of a particular grant/driver.
547///
548/// Capsules gain access to a [`GrantKernelData`] object by calling
549/// [`Grant::enter()`]. From there, they can schedule upcalls or access process
550/// buffers.
551///
552/// It is expected that this type will only exist as a short-lived stack
553/// allocation, so its size is not a significant concern.
554pub struct GrantKernelData<'a> {
555 /// A reference to the actual upcall slice stored in the grant.
556 upcalls: &'a [SavedUpcall],
557
558 /// A reference to the actual read only allow slice stored in the grant.
559 allow_ro: &'a [SavedAllowRo],
560
561 /// A reference to the actual read write allow slice stored in the grant.
562 allow_rw: &'a [SavedAllowRw],
563
564 /// We need to keep track of the driver number so we can properly identify
565 /// the Upcall that is called. We need to keep track of its source so we can
566 /// remove it if the Upcall is unsubscribed.
567 driver_num: usize,
568
569 /// A reference to the process that these upcalls are for. This is used for
570 /// actually scheduling the upcalls.
571 process: &'a dyn Process,
572}
573
574impl<'a> GrantKernelData<'a> {
575 /// Create a [`GrantKernelData`] object to provide a handle for capsules to
576 /// call Upcalls.
577 fn new(
578 upcalls: &'a [SavedUpcall],
579 allow_ro: &'a [SavedAllowRo],
580 allow_rw: &'a [SavedAllowRw],
581 driver_num: usize,
582 process: &'a dyn Process,
583 ) -> GrantKernelData<'a> {
584 Self {
585 upcalls,
586 allow_ro,
587 allow_rw,
588 driver_num,
589 process,
590 }
591 }
592
593 /// Schedule the specified upcall for the process with r0, r1, r2 as
594 /// provided values.
595 ///
596 /// Capsules call this function to schedule upcalls, and upcalls are
597 /// identified by the `subscribe_num`, which must match the subscribe number
598 /// used when the upcall was originally subscribed by a process.
599 /// `subscribe_num`s are indexed starting at zero.
600 pub fn schedule_upcall(
601 &self,
602 subscribe_num: usize,
603 r: (usize, usize, usize),
604 ) -> Result<(), UpcallError> {
605 // Implement `self.upcalls[subscribe_num]` without a chance of a panic.
606 self.upcalls.get(subscribe_num).map_or_else(
607 || {
608 if config::CONFIG.trace_syscalls {
609 debug!(
610 "[{:?}] schedule[{:#x}:{}] invalid subscribe_num",
611 self.process.processid(),
612 self.driver_num,
613 subscribe_num,
614 );
615 }
616
617 Err(UpcallError::InvalidSubscribeNum)
618 },
619 |saved_upcall| {
620 // We can create an `Upcall` object based on what is stored in
621 // the process grant and use that to add the upcall to the
622 // pending array for the process.
623 let upcall = Upcall::new(
624 self.process.processid(),
625 UpcallId {
626 subscribe_num,
627 driver_num: self.driver_num,
628 },
629 saved_upcall.appdata,
630 saved_upcall.fn_ptr,
631 );
632 upcall.schedule(self.process, r.0, r.1, r.2)
633 },
634 )
635 }
636
637 /// Search the work queue for the first pending operation with the given
638 /// `subscribe_num` and if one exists remove it from the task queue.
639 ///
640 /// Returns the associated [`Task`](crate::process::Task) if one was found, otherwise returns
641 /// [`None`].
642 pub fn remove_upcall(&self, subscribe_num: usize) -> Option<crate::process::Task> {
643 self.process.remove_upcall(UpcallId {
644 subscribe_num,
645 driver_num: self.driver_num,
646 })
647 }
648
649 /// Remove all scheduled upcalls with the given `subscribe_num` from the
650 /// task queue.
651 ///
652 /// Returns the number of removed upcalls.
653 pub fn remove_pending_upcalls(&self, subscribe_num: usize) -> usize {
654 self.process.remove_pending_upcalls(UpcallId {
655 subscribe_num,
656 driver_num: self.driver_num,
657 })
658 }
659
660 /// Returns a lifetime limited reference to the requested
661 /// [`ReadOnlyProcessBuffer`].
662 ///
663 /// The len of the returned [`ReadOnlyProcessBuffer`] must be checked by the
664 /// caller to ensure that a buffer has in fact been allocated. An
665 /// unallocated buffer will be returned as a [`ReadOnlyProcessBuffer`] of
666 /// length 0.
667 ///
668 /// The [`ReadOnlyProcessBuffer`] is only valid for as long as this object
669 /// is valid, i.e. the lifetime of the app enter closure.
670 ///
671 /// If the specified allow number is invalid, then a
672 /// [`crate::process::Error::AddressOutOfBounds`] will be returned. This
673 /// returns a [`crate::process::Error`] to allow for easy chaining of this
674 /// function with the `ReadOnlyProcessBuffer::enter()` function with
675 /// `and_then`.
676 pub fn get_readonly_processbuffer(
677 &self,
678 allow_ro_num: usize,
679 ) -> Result<ReadOnlyProcessBufferRef<'_>, crate::process::Error> {
680 self.allow_ro.get(allow_ro_num).map_or(
681 Err(crate::process::Error::AddressOutOfBounds),
682 |saved_ro| {
683 // # Safety
684 //
685 // This is the saved process buffer data has been validated to
686 // be wholly contained within this process before it was stored.
687 // The lifetime of the ReadOnlyProcessBuffer is bound to the
688 // lifetime of self, which correctly limits dereferencing this
689 // saved pointer to only when it is valid.
690 unsafe {
691 Ok(ReadOnlyProcessBufferRef::new(
692 saved_ro.ptr,
693 saved_ro.len,
694 self.process.processid(),
695 ))
696 }
697 },
698 )
699 }
700
701 /// Returns a lifetime limited reference to the requested
702 /// [`ReadWriteProcessBuffer`].
703 ///
704 /// The length of the returned [`ReadWriteProcessBuffer`] must be checked by
705 /// the caller to ensure that a buffer has in fact been allocated. An
706 /// unallocated buffer will be returned as a [`ReadWriteProcessBuffer`] of
707 /// length 0.
708 ///
709 /// The [`ReadWriteProcessBuffer`] is only value for as long as this object
710 /// is valid, i.e. the lifetime of the app enter closure.
711 ///
712 /// If the specified allow number is invalid, then a
713 /// [`crate::process::Error::AddressOutOfBounds`] will be returned. This
714 /// returns a [`crate::process::Error`] to allow for easy chaining of this
715 /// function with the `ReadWriteProcessBuffer::enter()` function with
716 /// `and_then`.
717 pub fn get_readwrite_processbuffer(
718 &self,
719 allow_rw_num: usize,
720 ) -> Result<ReadWriteProcessBufferRef<'_>, crate::process::Error> {
721 self.allow_rw.get(allow_rw_num).map_or(
722 Err(crate::process::Error::AddressOutOfBounds),
723 |saved_rw| {
724 // # Safety
725 //
726 // This is the saved process buffer data has been validated to
727 // be wholly contained within this process before it was stored.
728 // The lifetime of the ReadWriteProcessBuffer is bound to the
729 // lifetime of self, which correctly limits dereferencing this
730 // saved pointer to only when it is valid.
731 unsafe {
732 Ok(ReadWriteProcessBufferRef::new(
733 saved_rw.ptr,
734 saved_rw.len,
735 self.process.processid(),
736 ))
737 }
738 },
739 )
740 }
741}
742
743/// A minimal representation of an upcall, used for storing an upcall in a
744/// process' grant table without wasting memory duplicating information such as
745/// process ID.
746#[repr(C)]
747#[derive(Default)]
748struct SavedUpcall {
749 appdata: MachineRegister,
750 fn_ptr: CapabilityPtr,
751}
752
753/// A minimal representation of a read-only allow from app, used for storing a
754/// read-only allow in a process' kernel managed grant space without wasting
755/// memory duplicating information such as process ID.
756#[repr(C)]
757struct SavedAllowRo {
758 ptr: *const u8,
759 len: usize,
760}
761
762// This allow is still needed on the current stable compiler, but generates a warning
763// on the current nightly compiler, as of 05/18/2025. So allow this warning for now.
764// This can probably be fixed on the next nightly update.
765#[allow(clippy::derivable_impls)]
766impl Default for SavedAllowRo {
767 fn default() -> Self {
768 Self {
769 ptr: core::ptr::null(),
770 len: 0,
771 }
772 }
773}
774
775/// A minimal representation of a read-write allow from app, used for storing a
776/// read-write allow in a process' kernel managed grant space without wasting
777/// memory duplicating information such as process ID.
778#[repr(C)]
779struct SavedAllowRw {
780 ptr: *mut u8,
781 len: usize,
782}
783
784// This allow is still needed on the current stable compiler, but generates a warning
785// on the current nightly compiler, as of 05/18/2025. So allow this warning for now.
786// This can probably be fixed on the next nightly update.
787#[allow(clippy::derivable_impls)]
788impl Default for SavedAllowRw {
789 fn default() -> Self {
790 Self {
791 ptr: core::ptr::null_mut(),
792 len: 0,
793 }
794 }
795}
796
797/// Write the default value of T to every element of the array.
798///
799/// # Safety
800///
801/// The pointer must be well aligned and point to allocated memory that is
802/// writable for `size_of::<T> * num` bytes. No Rust references may exist to
803/// memory in the address range spanned by `base..base+num` at the time this
804/// function is called. The memory does not need to be initialized yet. If it
805/// already does contain initialized memory, then those contents will be
806/// overwritten without being `Drop`ed first.
807unsafe fn write_default_array<T: Default>(base: *mut T, num: usize) {
808 for i in 0..num {
809 base.add(i).write(T::default());
810 }
811}
812
813/// Enters the grant for the specified process. Caller must hold on to the grant
814/// lifetime guard while they accessing the memory in the layout (second
815/// element).
816fn enter_grant_kernel_managed(
817 process: &dyn Process,
818 driver_num: usize,
819) -> Result<EnteredGrantKernelManagedLayout<'_>, ErrorCode> {
820 let grant_num = process.lookup_grant_from_driver_num(driver_num)?;
821
822 // Check if the grant has been allocated, and if not we cannot enter this
823 // grant.
824 match process.grant_is_allocated(grant_num) {
825 Some(true) => { /* Allocated, nothing to do */ }
826 Some(false) => return Err(ErrorCode::NOMEM),
827 None => return Err(ErrorCode::FAIL),
828 }
829
830 // Return early if no grant.
831 let grant_base_ptr = process.enter_grant(grant_num).or(Err(ErrorCode::NOMEM))?;
832 // # Safety
833 //
834 // We know that this pointer is well aligned and initialized with meaningful
835 // data when the grant region was allocated.
836 let layout = unsafe {
837 EnteredGrantKernelManagedLayout::read_from_base(grant_base_ptr, process, grant_num)
838 };
839 Ok(layout)
840}
841
842/// Subscribe to an upcall by saving the upcall in the grant region for the
843/// process and returning the existing upcall for the same UpcallId.
844pub(crate) fn subscribe(
845 process: &dyn Process,
846 upcall: Upcall,
847) -> Result<Upcall, (Upcall, ErrorCode)> {
848 // Enter grant and keep it open until _grant_open goes out of scope.
849 let mut layout = match enter_grant_kernel_managed(process, upcall.upcall_id.driver_num) {
850 Ok(val) => val,
851 Err(e) => return Err((upcall, e)),
852 };
853
854 // Create the saved upcalls slice from the grant memory.
855 //
856 // # Safety
857 //
858 // This is safe because of how the grant was initially allocated and that
859 // because we were able to enter the grant the grant region must be valid
860 // and initialized. We are also holding the grant open until `_grant_open`
861 // goes out of scope.
862 let saved_upcalls_slice = layout.get_upcalls_slice();
863
864 // Index into the saved upcall slice to get the old upcall. Use .get in case
865 // userspace passed us a bad subscribe number.
866 match saved_upcalls_slice.get_mut(upcall.upcall_id.subscribe_num) {
867 Some(saved_upcall) => {
868 // Create an `Upcall` object with the old saved upcall.
869 let old_upcall = Upcall::new(
870 process.processid(),
871 upcall.upcall_id,
872 saved_upcall.appdata,
873 saved_upcall.fn_ptr,
874 );
875
876 // Overwrite the saved upcall with the new upcall.
877 saved_upcall.appdata = upcall.appdata;
878 saved_upcall.fn_ptr = upcall.fn_ptr;
879
880 // Success!
881 Ok(old_upcall)
882 }
883 None => Err((upcall, ErrorCode::NOSUPPORT)),
884 }
885}
886
887/// Stores the specified read-only process buffer in the kernel managed grant
888/// region for this process and driver. The previous read-only process buffer
889/// stored at the same allow_num id is returned.
890pub(crate) fn allow_ro(
891 process: &dyn Process,
892 driver_num: usize,
893 allow_num: usize,
894 buffer: ReadOnlyProcessBuffer,
895) -> Result<ReadOnlyProcessBuffer, (ReadOnlyProcessBuffer, ErrorCode)> {
896 // Enter grant and keep it open until `_grant_open` goes out of scope.
897 let mut layout = match enter_grant_kernel_managed(process, driver_num) {
898 Ok(val) => val,
899 Err(e) => return Err((buffer, e)),
900 };
901
902 // Create the saved allow ro slice from the grant memory.
903 //
904 // # Safety
905 //
906 // This is safe because of how the grant was initially allocated and that
907 // because we were able to enter the grant the grant region must be valid
908 // and initialized. We are also holding the grant open until _grant_open
909 // goes out of scope.
910 let saved_allow_ro_slice = layout.get_allow_ro_slice();
911
912 // Index into the saved slice to get the old value. Use .get in case
913 // userspace passed us a bad allow number.
914 match saved_allow_ro_slice.get_mut(allow_num) {
915 Some(saved) => {
916 // # Safety
917 //
918 // The pointer has already been validated to be within application
919 // memory before storing the values in the saved slice.
920 let old_allow =
921 unsafe { ReadOnlyProcessBuffer::new(saved.ptr, saved.len, process.processid()) };
922
923 // Replace old values with current buffer.
924 let (ptr, len) = buffer.consume();
925 saved.ptr = ptr;
926 saved.len = len;
927
928 // Success!
929 Ok(old_allow)
930 }
931 None => Err((buffer, ErrorCode::NOSUPPORT)),
932 }
933}
934
935/// Stores the specified read-write process buffer in the kernel managed grant
936/// region for this process and driver. The previous read-write process buffer
937/// stored at the same allow_num id is returned.
938pub(crate) fn allow_rw(
939 process: &dyn Process,
940 driver_num: usize,
941 allow_num: usize,
942 buffer: ReadWriteProcessBuffer,
943) -> Result<ReadWriteProcessBuffer, (ReadWriteProcessBuffer, ErrorCode)> {
944 // Enter grant and keep it open until `_grant_open` goes out of scope.
945 let mut layout = match enter_grant_kernel_managed(process, driver_num) {
946 Ok(val) => val,
947 Err(e) => return Err((buffer, e)),
948 };
949
950 // Create the saved allow rw slice from the grant memory.
951 //
952 // # Safety
953 //
954 // This is safe because of how the grant was initially allocated and that
955 // because we were able to enter the grant the grant region must be valid
956 // and initialized. We are also holding the grant open until `_grant_open`
957 // goes out of scope.
958 let saved_allow_rw_slice = layout.get_allow_rw_slice();
959
960 // Index into the saved slice to get the old value. Use .get in case
961 // userspace passed us a bad allow number.
962 match saved_allow_rw_slice.get_mut(allow_num) {
963 Some(saved) => {
964 // # Safety
965 //
966 // The pointer has already been validated to be within application
967 // memory before storing the values in the saved slice.
968 let old_allow =
969 unsafe { ReadWriteProcessBuffer::new(saved.ptr, saved.len, process.processid()) };
970
971 // Replace old values with current buffer.
972 let (ptr, len) = buffer.consume();
973 saved.ptr = ptr;
974 saved.len = len;
975
976 // Success!
977 Ok(old_allow)
978 }
979 None => Err((buffer, ErrorCode::NOSUPPORT)),
980 }
981}
982
983/// An instance of a grant allocated for a particular process.
984///
985/// [`ProcessGrant`] is a handle to an instance of a grant that has been
986/// allocated in a specific process's grant region. A [`ProcessGrant`]
987/// guarantees that the memory for the grant has been allocated in the process's
988/// memory.
989///
990/// This is created from a [`Grant`] when that grant is entered for a specific
991/// process.
992pub struct ProcessGrant<
993 'a,
994 T: 'a,
995 Upcalls: UpcallSize,
996 AllowROs: AllowRoSize,
997 AllowRWs: AllowRwSize,
998> {
999 /// The process the grant is applied to.
1000 ///
1001 /// We use a reference here because instances of [`ProcessGrant`] are very
1002 /// short lived. They only exist while a [`Grant`] is being entered, so we
1003 /// can be sure the process still exists while a `ProcessGrant` exists. No
1004 /// [`ProcessGrant`] can be stored.
1005 process: &'a dyn Process,
1006
1007 /// The syscall driver number this grant is associated with.
1008 driver_num: usize,
1009
1010 /// The identifier of the Grant this is applied for.
1011 grant_num: usize,
1012
1013 /// Used to store Rust types for grant.
1014 _phantom: PhantomData<(T, Upcalls, AllowROs, AllowRWs)>,
1015}
1016
1017impl<'a, T: Default, Upcalls: UpcallSize, AllowROs: AllowRoSize, AllowRWs: AllowRwSize>
1018 ProcessGrant<'a, T, Upcalls, AllowROs, AllowRWs>
1019{
1020 /// Create a [`ProcessGrant`] for the given Grant in the given Process's
1021 /// grant region.
1022 ///
1023 /// If the grant in this process has not been setup before this will attempt
1024 /// to allocate the memory from the process's grant region.
1025 ///
1026 /// # Return
1027 ///
1028 /// If the grant is already allocated or could be allocated, and the process
1029 /// is valid, this returns `Ok(ProcessGrant)`. Otherwise it returns a
1030 /// relevant error.
1031 fn new(
1032 grant: &Grant<T, Upcalls, AllowROs, AllowRWs>,
1033 processid: ProcessId,
1034 ) -> Result<Self, Error> {
1035 // Moves non-generic code from new() into non-generic function to reduce
1036 // code bloat from the generic function being monomorphized, as it is
1037 // common to have over 50 copies of Grant::enter() in a Tock kernel (and
1038 // thus 50+ copies of this function). The returned Option indicates if
1039 // the returned pointer still needs to be initialized (in the case where
1040 // the grant was only just allocated).
1041 fn new_inner<'a>(
1042 grant_num: usize,
1043 driver_num: usize,
1044 grant_t_size: GrantDataSize,
1045 grant_t_align: GrantDataAlign,
1046 num_upcalls: UpcallItems,
1047 num_allow_ros: AllowRoItems,
1048 num_allow_rws: AllowRwItems,
1049 processid: ProcessId,
1050 ) -> Result<(Option<NonNull<u8>>, &'a dyn Process), Error> {
1051 // Here is an example of how the grants are laid out in the grant
1052 // region of process's memory:
1053 //
1054 // Mem. Addr.
1055 // 0x0040000 ┌────────────────────────────────────
1056 // │ DriverNumN [0x1]
1057 // │ GrantPointerN [0x003FFC8]
1058 // │ ...
1059 // │ DriverNum1 [0x60000]
1060 // │ GrantPointer1 [0x003FFC0]
1061 // │ DriverNum0
1062 // │ GrantPointer0 [0x0000000 (NULL)]
1063 // ├────────────────────────────────────
1064 // │ Process Control Block
1065 // 0x003FFE0 ├──────────────────────────────────── Grant Region ┐
1066 // │ GrantDataN │
1067 // 0x003FFC8 ├──────────────────────────────────── │
1068 // │ GrantData1 ▼
1069 // 0x003FF20 ├────────────────────────────────────
1070 // │
1071 // │ --unallocated--
1072 // │
1073 // └────────────────────────────────────
1074 //
1075 // An array of pointers (one per possible grant region) point to
1076 // where the actual grant memory is allocated inside of the process.
1077 // The grant memory is not allocated until the actual grant region
1078 // is actually used.
1079
1080 let process = processid
1081 .kernel
1082 .get_process(processid)
1083 .ok_or(Error::NoSuchApp)?;
1084
1085 // Check if the grant is allocated. If not, we allocate it process
1086 // memory first. We then create an `ProcessGrant` object for this
1087 // grant.
1088 if let Some(is_allocated) = process.grant_is_allocated(grant_num) {
1089 if !is_allocated {
1090 // Calculate the alignment and size for entire grant region.
1091 let alloc_align = EnteredGrantKernelManagedLayout::grant_align(grant_t_align);
1092 let alloc_size = EnteredGrantKernelManagedLayout::grant_size(
1093 num_upcalls,
1094 num_allow_ros,
1095 num_allow_rws,
1096 grant_t_size,
1097 grant_t_align,
1098 );
1099
1100 // Allocate grant, the memory is still uninitialized though.
1101 if process
1102 .allocate_grant(grant_num, driver_num, alloc_size, alloc_align)
1103 .is_err()
1104 {
1105 return Err(Error::OutOfMemory);
1106 }
1107
1108 let grant_ptr = process.enter_grant(grant_num)?;
1109
1110 // Create a layout from the counts we have and initialize
1111 // all memory so it is valid in the future to read as a
1112 // reference.
1113 //
1114 // # Safety
1115 //
1116 // - The grant base pointer is well aligned, yet does not
1117 // have initialized data.
1118 // - The pointer points to a large enough space to correctly
1119 // write to is guaranteed by alloc of size
1120 // `EnteredGrantKernelManagedLayout::grant_size`.
1121 // - There are no proper rust references that map to these
1122 // addresses.
1123 unsafe {
1124 let _layout = EnteredGrantKernelManagedLayout::initialize_from_counts(
1125 grant_ptr,
1126 num_upcalls,
1127 num_allow_ros,
1128 num_allow_rws,
1129 process,
1130 grant_num,
1131 );
1132 }
1133
1134 // # Safety
1135 //
1136 // The grant pointer points to an alloc that is alloc_size
1137 // large and is at least as aligned as grant_t_align.
1138 unsafe {
1139 Ok((
1140 Some(EnteredGrantKernelManagedLayout::offset_of_grant_data_t(
1141 grant_ptr,
1142 alloc_size,
1143 grant_t_size,
1144 )),
1145 process,
1146 ))
1147 }
1148 } else {
1149 // T was already allocated, outer function should not
1150 // initialize T.
1151 Ok((None, process))
1152 }
1153 } else {
1154 // Cannot use the grant region in any way if the process is
1155 // inactive.
1156 Err(Error::InactiveApp)
1157 }
1158 }
1159
1160 // Handle the bulk of the work in a function which is not templated.
1161 let (opt_raw_grant_ptr_nn, process) = new_inner(
1162 grant.grant_num,
1163 grant.driver_num,
1164 GrantDataSize(size_of::<T>()),
1165 GrantDataAlign(align_of::<T>()),
1166 UpcallItems(Upcalls::COUNT),
1167 AllowRoItems(AllowROs::COUNT),
1168 AllowRwItems(AllowRWs::COUNT),
1169 processid,
1170 )?;
1171
1172 // We can now do the initialization of T object if necessary.
1173 if let Some(allocated_ptr) = opt_raw_grant_ptr_nn {
1174 // Grant type T
1175 //
1176 // # Safety
1177 //
1178 // This is safe because:
1179 //
1180 // 1. The pointer address is valid. The pointer is allocated
1181 // statically in process memory, and will exist for as long
1182 // as the process does. The grant is only accessible while
1183 // the process is still valid.
1184 //
1185 // 2. The pointer is correctly aligned. The newly allocated
1186 // grant is aligned for type T, and there is padding inserted
1187 // between the upcall array and the T object such that the T
1188 // object starts a multiple of `align_of<T>` from the
1189 // beginning of the allocation.
1190 unsafe {
1191 // Convert untyped `*mut u8` allocation to allocated type.
1192 let new_region = NonNull::cast::<T>(allocated_ptr);
1193 // We use `ptr::write` to avoid `Drop`ping the uninitialized
1194 // memory in case `T` implements the `Drop` trait.
1195 write(new_region.as_ptr(), T::default());
1196 }
1197 }
1198
1199 // We have ensured the grant is already allocated or was just allocated,
1200 // so we can create and return the `ProcessGrant` type.
1201 Ok(ProcessGrant {
1202 process,
1203 driver_num: grant.driver_num,
1204 grant_num: grant.grant_num,
1205 _phantom: PhantomData,
1206 })
1207 }
1208
1209 /// Return a [`ProcessGrant`] for a grant in a process if the process is
1210 /// valid and that process grant has already been allocated, or `None`
1211 /// otherwise.
1212 fn new_if_allocated(
1213 grant: &Grant<T, Upcalls, AllowROs, AllowRWs>,
1214 process: &'a dyn Process,
1215 ) -> Option<Self> {
1216 if let Some(is_allocated) = process.grant_is_allocated(grant.grant_num) {
1217 if is_allocated {
1218 Some(ProcessGrant {
1219 process,
1220 driver_num: grant.driver_num,
1221 grant_num: grant.grant_num,
1222 _phantom: PhantomData,
1223 })
1224 } else {
1225 // Grant has not been allocated.
1226 None
1227 }
1228 } else {
1229 // Process is invalid.
1230 None
1231 }
1232 }
1233
1234 /// Return the [`ProcessId`] of the process this [`ProcessGrant`] is
1235 /// associated with.
1236 pub fn processid(&self) -> ProcessId {
1237 self.process.processid()
1238 }
1239
1240 /// Run a function with access to the memory in the related process for the
1241 /// related Grant. This also provides access to any associated Upcalls and
1242 /// allowed buffers stored with the grant.
1243 ///
1244 /// This is "entering" the grant region, and the _only_ time when the
1245 /// contents of a grant region can be accessed.
1246 ///
1247 /// Note, a grant can only be entered once at a time. Attempting to call
1248 /// `.enter()` on a grant while it is already entered will result in a
1249 /// `panic!()`. See the comment in `access_grant()` for more information.
1250 pub fn enter<F, R>(self, fun: F) -> R
1251 where
1252 F: FnOnce(&mut GrantData<T>, &GrantKernelData) -> R,
1253 {
1254 // # `unwrap()` Safety
1255 //
1256 // `access_grant()` can only return `None` if the grant is already
1257 // entered. Since we are asking for a panic!() if the grant is entered,
1258 // `access_grant()` function will never return `None`.
1259 self.access_grant(fun, true).unwrap()
1260 }
1261
1262 /// Run a function with access to the data in the related process for the
1263 /// related Grant only if that grant region is not already entered. If the
1264 /// grant is already entered silently skip it. Also provide access to
1265 /// associated Upcalls.
1266 ///
1267 /// **You almost certainly should use `.enter()` rather than
1268 /// `.try_enter()`.**
1269 ///
1270 /// While the `.enter()` version can panic, that panic likely indicates a
1271 /// bug in the code and not a condition that should be handled. For example,
1272 /// this benign looking code is wrong:
1273 ///
1274 /// ```ignore
1275 /// self.apps.enter(thisapp, |app_grant, _| {
1276 /// // Update state in the grant region of `thisapp`. Also, mark that
1277 /// // `thisapp` needs to run again.
1278 /// app_grant.runnable = true;
1279 ///
1280 /// // Now, check all apps to see if any are ready to run.
1281 /// let mut work_left_to_do = false;
1282 /// self.apps.iter().each(|other_app| {
1283 /// other_app.enter(|other_app_grant, _| { // ERROR! This leads to a
1284 /// if other_app_grant.runnable { // grant being entered
1285 /// work_left_to_do = true; // twice!
1286 /// }
1287 /// })
1288 /// })
1289 /// })
1290 /// ```
1291 ///
1292 /// The example is wrong because it tries to iterate across all grant
1293 /// regions while one of them is already entered. This will lead to a grant
1294 /// region being entered twice which violates Rust's memory restrictions and
1295 /// is undefined behavior.
1296 ///
1297 /// However, since the example uses `.enter()` on the iteration, Tock will
1298 /// panic when the grant is entered for the second time, notifying the
1299 /// developer that something is wrong. The fix is to exit out of the first
1300 /// `.enter()` before attempting to iterate over the grant for all
1301 /// processes.
1302 ///
1303 /// However, if the example used `.try_enter()` in the iter loop, there
1304 /// would be no panic, but the already entered grant would be silently
1305 /// skipped. This can hide subtle bugs if the skipped grant is only relevant
1306 /// in certain cases.
1307 ///
1308 /// Therefore, only use `try_enter()` if you are sure you want to skip the
1309 /// already entered grant. Cases for this are rare.
1310 ///
1311 /// ## Return
1312 ///
1313 /// Returns `None` if the grant is already entered. Otherwise returns
1314 /// `Some(fun())`.
1315 pub fn try_enter<F, R>(self, fun: F) -> Option<R>
1316 where
1317 F: FnOnce(&mut GrantData<T>, &GrantKernelData) -> R,
1318 {
1319 self.access_grant(fun, false)
1320 }
1321
1322 /// Run a function with access to the memory in the related process for the
1323 /// related Grant. Also provide this function with access to any associated
1324 /// Upcalls and an allocator for allocating additional memory in the
1325 /// process's grant region.
1326 ///
1327 /// This is "entering" the grant region, and the _only_ time when the
1328 /// contents of a grant region can be accessed.
1329 ///
1330 /// Note, a grant can only be entered once at a time. Attempting to call
1331 /// `.enter()` on a grant while it is already entered will result in a
1332 /// panic!()`. See the comment in `access_grant()` for more information.
1333 pub fn enter_with_allocator<F, R>(self, fun: F) -> R
1334 where
1335 F: FnOnce(&mut GrantData<T>, &GrantKernelData, &mut GrantRegionAllocator) -> R,
1336 {
1337 // # `unwrap()` Safety
1338 //
1339 // `access_grant()` can only return `None` if the grant is already
1340 // entered. Since we are asking for a panic!() if the grant is entered,
1341 // `access_grant()` function will never return `None`.
1342 self.access_grant_with_allocator(fun, true).unwrap()
1343 }
1344
1345 /// Access the [`ProcessGrant`] memory and run a closure on the process's
1346 /// grant memory.
1347 ///
1348 /// If `panic_on_reenter` is `true`, this will panic if the grant region is
1349 /// already currently entered. If `panic_on_reenter` is `false`, this will
1350 /// return `None` if the grant region is entered and do nothing.
1351 fn access_grant<F, R>(self, fun: F, panic_on_reenter: bool) -> Option<R>
1352 where
1353 F: FnOnce(&mut GrantData<T>, &GrantKernelData) -> R,
1354 {
1355 self.access_grant_with_allocator(
1356 |grant_data, kernel_data, _allocator| fun(grant_data, kernel_data),
1357 panic_on_reenter,
1358 )
1359 }
1360
1361 /// Access the [`ProcessGrant`] memory and run a closure on the process's
1362 /// grant memory.
1363 ///
1364 /// If `panic_on_reenter` is `true`, this will panic if the grant region is
1365 /// already currently entered. If `panic_on_reenter` is `false`, this will
1366 /// return `None` if the grant region is entered and do nothing.
1367 fn access_grant_with_allocator<F, R>(self, fun: F, panic_on_reenter: bool) -> Option<R>
1368 where
1369 F: FnOnce(&mut GrantData<T>, &GrantKernelData, &mut GrantRegionAllocator) -> R,
1370 {
1371 // Access the grant that is in process memory. This can only fail if
1372 // the grant is already entered.
1373 let grant_ptr = self
1374 .process
1375 .enter_grant(self.grant_num)
1376 .map_err(|_err| {
1377 // If we get an error it is because the grant is already
1378 // entered. `process.enter_grant()` can fail for several
1379 // reasons, but only the double enter case can happen once a
1380 // grant has been applied. The other errors would be detected
1381 // earlier (i.e. before the grant can be applied).
1382
1383 // If `panic_on_reenter` is false, we skip this error and do
1384 // nothing with this grant.
1385 if !panic_on_reenter {
1386 return;
1387 }
1388
1389 // If `enter_grant` fails, we panic!() to notify the developer
1390 // that they tried to enter the same grant twice which is
1391 // prohibited because it would result in two mutable references
1392 // existing for the same memory. This preserves type correctness
1393 // (but does crash the system).
1394 //
1395 // ## Explanation and Rationale
1396 //
1397 // This panic represents a tradeoff. While it is undesirable to
1398 // have the potential for a runtime crash in this grant region
1399 // code, it balances usability with type correctness. The
1400 // challenge is that calling `self.apps.iter()` is a common
1401 // pattern in capsules to access the grant region of every app
1402 // that is using the capsule, and sometimes it is intuitive to
1403 // call that inside of a `self.apps.enter(processid, |app| {...})`
1404 // closure. However, `.enter()` means that app's grant region is
1405 // entered, and then a naive `.iter()` would re-enter the grant
1406 // region and cause undefined behavior. We considered different
1407 // options to resolve this.
1408 //
1409 // 1. Have `.iter()` only iterate over grant regions which are
1410 // not entered. This avoids the bug, but could lead to
1411 // unexpected behavior, as `self.apps.iter()` will do
1412 // different things depending on where in a capsule it is
1413 // called.
1414 // 2. Have the compiler detect when `.iter()` is called when a
1415 // grant region has already been entered. We don't know of a
1416 // viable way to implement this.
1417 // 3. Panic if `.iter()` is called when a grant is already
1418 // entered.
1419 //
1420 // We decided on option 3 because it balances minimizing
1421 // surprises (`self.apps.iter()` will always iterate all grants)
1422 // while also protecting against the bug. We expect that any
1423 // code that attempts to call `self.apps.iter()` after calling
1424 // `.enter()` will immediately encounter this `panic!()` and
1425 // have to be refactored before any tests will be successful.
1426 // Therefore, this `panic!()` should only occur at
1427 // development/testing time.
1428 //
1429 // ## How to fix this error
1430 //
1431 // If you are seeing this panic, you need to refactor your
1432 // capsule to not call `.iter()` or `.each()` from inside a
1433 // `.enter()` closure. That is, you need to close the grant
1434 // region you are currently in before trying to iterate over all
1435 // grant regions.
1436 panic!("Attempted to re-enter a grant region.");
1437 })
1438 .ok()?;
1439 let grant_t_align = GrantDataAlign(align_of::<T>());
1440 let grant_t_size = GrantDataSize(size_of::<T>());
1441
1442 let alloc_size = EnteredGrantKernelManagedLayout::grant_size(
1443 UpcallItems(Upcalls::COUNT),
1444 AllowRoItems(AllowROs::COUNT),
1445 AllowRwItems(AllowRWs::COUNT),
1446 grant_t_size,
1447 grant_t_align,
1448 );
1449
1450 // Parse layout of entire grant allocation using the known base pointer.
1451 //
1452 // # Safety
1453 //
1454 // Grant pointer is well aligned and points to initialized data.
1455 let layout = unsafe {
1456 EnteredGrantKernelManagedLayout::read_from_base(grant_ptr, self.process, self.grant_num)
1457 };
1458
1459 // Get references to all of the saved upcall data.
1460 //
1461 // # Safety
1462 //
1463 // - Pointer is well aligned and initialized with data from Self::new()
1464 // call.
1465 // - Data will not be modified externally while this immutable reference
1466 // is alive.
1467 // - Data is accessible for the entire duration of this immutable
1468 // reference.
1469 // - No other mutable reference to this memory exists concurrently.
1470 // Mutable reference to this memory are only created through the
1471 // kernel in the syscall interface which is serialized in time with
1472 // this call.
1473 let (saved_upcalls_slice, saved_allow_ro_slice, saved_allow_rw_slice) =
1474 layout.get_resource_slices();
1475 let grant_data = unsafe {
1476 EnteredGrantKernelManagedLayout::offset_of_grant_data_t(
1477 grant_ptr,
1478 alloc_size,
1479 grant_t_size,
1480 )
1481 .cast()
1482 .as_mut()
1483 };
1484
1485 // Create a wrapped objects that are passed to functor.
1486 let mut grant_data = GrantData::new(grant_data);
1487 let kernel_data = GrantKernelData::new(
1488 saved_upcalls_slice,
1489 saved_allow_ro_slice,
1490 saved_allow_rw_slice,
1491 self.driver_num,
1492 self.process,
1493 );
1494 // Setup an allocator in case the capsule needs additional memory in the
1495 // grant space.
1496 let mut allocator = GrantRegionAllocator {
1497 processid: self.process.processid(),
1498 };
1499
1500 // Call functor and pass back value.
1501 Some(fun(&mut grant_data, &kernel_data, &mut allocator))
1502 }
1503}
1504
1505/// Grant which was allocated from the kernel-owned grant region in a specific
1506/// process's memory, separately from a normal `Grant`.
1507///
1508/// A [`CustomGrant`] allows a capsule to allocate additional memory on behalf
1509/// of a process.
1510pub struct CustomGrant<T> {
1511 /// An identifier for this custom grant within a process's grant region.
1512 ///
1513 /// Here, this is an opaque reference that Process uses to access the
1514 /// custom grant allocation. This setup ensures that Process owns the grant
1515 /// memory.
1516 identifier: ProcessCustomGrantIdentifier,
1517
1518 /// Identifier for the process where this custom grant is allocated.
1519 processid: ProcessId,
1520
1521 /// Used to keep the Rust type of the grant.
1522 _phantom: PhantomData<T>,
1523}
1524
1525impl<T> CustomGrant<T> {
1526 /// Creates a new [`CustomGrant`].
1527 fn new(identifier: ProcessCustomGrantIdentifier, processid: ProcessId) -> Self {
1528 CustomGrant {
1529 identifier,
1530 processid,
1531 _phantom: PhantomData,
1532 }
1533 }
1534
1535 /// Helper function to get the [`ProcessId`] from the custom grant.
1536 pub fn processid(&self) -> ProcessId {
1537 self.processid
1538 }
1539
1540 /// Gives access to inner data within the given closure.
1541 ///
1542 /// If the process has since been restarted or crashed, or the memory is
1543 /// otherwise no longer present, then this function will not call the given
1544 /// closure, and will instead directly return `Err(Error::NoSuchApp)`.
1545 ///
1546 /// Because this function requires `&mut self`, it should be impossible to
1547 /// access the inner data of a given `CustomGrant` reentrantly. Thus the
1548 /// reentrance detection we use for non-custom grants is not needed here.
1549 pub fn enter<F, R>(&self, fun: F) -> Result<R, Error>
1550 where
1551 F: FnOnce(GrantData<'_, T>) -> R,
1552 {
1553 // Verify that the process this CustomGrant was allocated within still
1554 // exists.
1555 self.processid
1556 .kernel
1557 .process_map_or(Err(Error::NoSuchApp), self.processid, |process| {
1558 // App is valid.
1559
1560 // Now try to access the custom grant memory.
1561 let grant_ptr = process.enter_custom_grant(self.identifier)?;
1562
1563 // # Safety
1564 //
1565 // `grant_ptr` must be a valid pointer and there must not exist
1566 // any other references to the same memory. We verify the
1567 // pointer is valid and aligned when the memory is allocated and
1568 // `CustomGrant` is created. We are sure that there are no
1569 // other references because the only way to create a reference
1570 // is using this `enter()` function, and it can only be called
1571 // once (because of the `&mut self` requirement).
1572 let custom_grant = unsafe { &mut *(grant_ptr as *mut T) };
1573 let borrowed = GrantData::new(custom_grant);
1574 Ok(fun(borrowed))
1575 })
1576 }
1577}
1578
1579/// Tool for allocating additional memory regions in a process's grant region.
1580///
1581/// This is optionally provided along with a grant so that if a capsule needs
1582/// per-process dynamic allocation it can allocate additional memory.
1583pub struct GrantRegionAllocator {
1584 /// The process the allocator will allocate memory from.
1585 processid: ProcessId,
1586}
1587
1588impl GrantRegionAllocator {
1589 /// Allocates a new [`CustomGrant`] initialized using the given closure.
1590 ///
1591 /// The closure will be called exactly once, and the result will be used to
1592 /// initialize the owned value.
1593 ///
1594 /// This interface was chosen instead of a simple `alloc(val)` as it's
1595 /// much more likely to optimize out all stack intermediates. This
1596 /// helps to prevent stack overflows when allocating large values.
1597 ///
1598 /// # Panic Safety
1599 ///
1600 /// If `init` panics, the freshly allocated memory may leak.
1601 pub fn alloc_with<T, F>(&self, init: F) -> Result<CustomGrant<T>, Error>
1602 where
1603 F: FnOnce() -> T,
1604 {
1605 let (custom_grant_identifier, typed_ptr) = self.alloc_raw::<T>()?;
1606
1607 // # Safety
1608 //
1609 // Writing to this pointer is safe as long as the pointer is valid
1610 // and aligned. `alloc_raw()` guarantees these constraints are met.
1611 unsafe {
1612 // We use `ptr::write` to avoid `Drop`ping the uninitialized memory
1613 // in case `T` implements the `Drop` trait.
1614 write(typed_ptr.as_ptr(), init());
1615 }
1616
1617 Ok(CustomGrant::new(custom_grant_identifier, self.processid))
1618 }
1619
1620 /// Allocates a slice of n instances of a given type. Each instance is
1621 /// initialized using the provided function.
1622 ///
1623 /// The provided function will be called exactly `n` times, and will be
1624 /// passed the index it's initializing, from `0` through `NUM_ITEMS - 1`.
1625 ///
1626 /// # Panic Safety
1627 ///
1628 /// If `val_func` panics, the freshly allocated memory and any values
1629 /// already written will be leaked.
1630 pub fn alloc_n_with<T, F, const NUM_ITEMS: usize>(
1631 &self,
1632 mut init: F,
1633 ) -> Result<CustomGrant<[T; NUM_ITEMS]>, Error>
1634 where
1635 F: FnMut(usize) -> T,
1636 {
1637 let (custom_grant_identifier, typed_ptr) = self.alloc_n_raw::<T>(NUM_ITEMS)?;
1638
1639 for i in 0..NUM_ITEMS {
1640 // # Safety
1641 //
1642 // The allocate function guarantees that `ptr` points to memory
1643 // large enough to allocate `num_items` copies of the object.
1644 unsafe {
1645 write(typed_ptr.as_ptr().add(i), init(i));
1646 }
1647 }
1648
1649 Ok(CustomGrant::new(custom_grant_identifier, self.processid))
1650 }
1651
1652 /// Allocates uninitialized grant memory appropriate to store a `T`.
1653 ///
1654 /// The caller must initialize the memory.
1655 ///
1656 /// Also returns a ProcessCustomGrantIdentifier to access the memory later.
1657 fn alloc_raw<T>(&self) -> Result<(ProcessCustomGrantIdentifier, NonNull<T>), Error> {
1658 self.alloc_n_raw::<T>(1)
1659 }
1660
1661 /// Allocates space for a dynamic number of items.
1662 ///
1663 /// The caller is responsible for initializing the returned memory.
1664 ///
1665 /// Returns memory appropriate for storing `num_items` contiguous instances
1666 /// of `T` and a ProcessCustomGrantIdentifier to access the memory later.
1667 fn alloc_n_raw<T>(
1668 &self,
1669 num_items: usize,
1670 ) -> Result<(ProcessCustomGrantIdentifier, NonNull<T>), Error> {
1671 let (custom_grant_identifier, raw_ptr) =
1672 self.alloc_n_raw_inner(num_items, size_of::<T>(), align_of::<T>())?;
1673 let typed_ptr = NonNull::cast::<T>(raw_ptr);
1674
1675 Ok((custom_grant_identifier, typed_ptr))
1676 }
1677
1678 /// Helper to reduce code bloat by avoiding monomorphization.
1679 fn alloc_n_raw_inner(
1680 &self,
1681 num_items: usize,
1682 single_alloc_size: usize,
1683 alloc_align: usize,
1684 ) -> Result<(ProcessCustomGrantIdentifier, NonNull<u8>), Error> {
1685 let alloc_size = single_alloc_size
1686 .checked_mul(num_items)
1687 .ok_or(Error::OutOfMemory)?;
1688 self.processid
1689 .kernel
1690 .process_map_or(Err(Error::NoSuchApp), self.processid, |process| {
1691 process
1692 .allocate_custom_grant(alloc_size, alloc_align)
1693 .map_or(
1694 Err(Error::OutOfMemory),
1695 |(custom_grant_identifier, raw_ptr)| Ok((custom_grant_identifier, raw_ptr)),
1696 )
1697 })
1698 }
1699}
1700
1701/// Type for storing an object of type T in process memory that is only
1702/// accessible by the kernel.
1703///
1704/// A single [`Grant`] can allocate space for one object of type T for each
1705/// process on the board. Each allocated object will reside in the grant region
1706/// belonging to the process that the object is allocated for. The [`Grant`]
1707/// type is used to get access to [`ProcessGrant`]s, which are tied to a
1708/// specific process and provide access to the memory object allocated for that
1709/// process.
1710pub struct Grant<T: Default, Upcalls: UpcallSize, AllowROs: AllowRoSize, AllowRWs: AllowRwSize> {
1711 /// Hold a reference to the core kernel so we can iterate processes.
1712 pub(crate) kernel: &'static Kernel,
1713
1714 /// Keep track of the syscall driver number assigned to the capsule that is
1715 /// using this grant. This allows us to uniquely identify upcalls stored in
1716 /// this grant.
1717 driver_num: usize,
1718
1719 /// The identifier for this grant. Having an identifier allows the Process
1720 /// implementation to lookup the memory for this grant in the specific
1721 /// process.
1722 grant_num: usize,
1723
1724 /// Used to store the Rust types for grant.
1725 ptr: PhantomData<(T, Upcalls, AllowROs, AllowRWs)>,
1726}
1727
1728impl<T: Default, Upcalls: UpcallSize, AllowROs: AllowRoSize, AllowRWs: AllowRwSize>
1729 Grant<T, Upcalls, AllowROs, AllowRWs>
1730{
1731 /// Create a new [`Grant`] type which allows a capsule to store
1732 /// process-specific data for each process in the process's memory region.
1733 ///
1734 /// This must only be called from the main kernel so that it can ensure that
1735 /// `grant_index` is a valid index.
1736 pub(crate) fn new(kernel: &'static Kernel, driver_num: usize, grant_index: usize) -> Self {
1737 Self {
1738 kernel,
1739 driver_num,
1740 grant_num: grant_index,
1741 ptr: PhantomData,
1742 }
1743 }
1744
1745 /// Enter the grant for a specific process.
1746 ///
1747 /// This creates a [`ProcessGrant`] which is a handle for a grant allocated
1748 /// for a specific process. Then, that [`ProcessGrant`] is entered and the
1749 /// provided closure is run with access to the memory in the grant region.
1750 pub fn enter<F, R>(&self, processid: ProcessId, fun: F) -> Result<R, Error>
1751 where
1752 F: FnOnce(&mut GrantData<T>, &GrantKernelData) -> R,
1753 {
1754 let pg = ProcessGrant::new(self, processid)?;
1755
1756 // If we have managed to create an `ProcessGrant`, all we need
1757 // to do is actually access the memory and run the
1758 // capsule-provided closure. This can only fail if the grant is
1759 // already entered, at which point the kernel will panic.
1760 Ok(pg.enter(fun))
1761 }
1762
1763 /// Enter the grant for a specific process with access to an allocator.
1764 ///
1765 /// This creates an [`ProcessGrant`] which is a handle for a grant allocated
1766 /// for a specific process. Then, that [`ProcessGrant`] is entered and the
1767 /// provided closure is run with access to the memory in the grant region.
1768 ///
1769 /// The allocator allows the caller to dynamically allocate additional
1770 /// memory in the process's grant region.
1771 pub fn enter_with_allocator<F, R>(&self, processid: ProcessId, fun: F) -> Result<R, Error>
1772 where
1773 F: FnOnce(&mut GrantData<T>, &GrantKernelData, &mut GrantRegionAllocator) -> R,
1774 {
1775 // Get the `ProcessGrant` for the process, possibly needing to
1776 // actually allocate the memory in the process's grant region to
1777 // do so. This can fail for a variety of reasons, and if so we
1778 // return the error to the capsule.
1779 let pg = ProcessGrant::new(self, processid)?;
1780
1781 // If we have managed to create an `ProcessGrant`, all we need
1782 // to do is actually access the memory and run the
1783 // capsule-provided closure. This can only fail if the grant is
1784 // already entered, at which point the kernel will panic.
1785 Ok(pg.enter_with_allocator(fun))
1786 }
1787
1788 /// Run a function on the grant for each active process if the grant has
1789 /// been allocated for that process.
1790 ///
1791 /// This will silently skip any process where the grant has not previously
1792 /// been allocated. This will also silently skip any invalid processes.
1793 ///
1794 /// Calling this function when an [`ProcessGrant`] for a process is
1795 /// currently entered will result in a panic.
1796 pub fn each<F>(&self, mut fun: F)
1797 where
1798 F: FnMut(ProcessId, &mut GrantData<T>, &GrantKernelData),
1799 {
1800 // Create a the iterator across `ProcessGrant`s for each process.
1801 for pg in self.iter() {
1802 let processid = pg.processid();
1803 // Since we iterating, there is no return value we need to worry
1804 // about.
1805 pg.enter(|data, upcalls| fun(processid, data, upcalls));
1806 }
1807 }
1808
1809 /// Get an iterator over all processes and their active grant regions for
1810 /// this particular grant.
1811 ///
1812 /// Calling this function when an [`ProcessGrant`] for a process is
1813 /// currently entered will result in a panic.
1814 pub fn iter(&self) -> Iter<'_, T, Upcalls, AllowROs, AllowRWs> {
1815 Iter {
1816 grant: self,
1817 subiter: self.kernel.get_process_iter(),
1818 }
1819 }
1820}
1821
1822/// Type to iterate [`ProcessGrant`]s across processes.
1823pub struct Iter<
1824 'a,
1825 T: 'a + Default,
1826 Upcalls: UpcallSize,
1827 AllowROs: AllowRoSize,
1828 AllowRWs: AllowRwSize,
1829> {
1830 /// The grant type to use.
1831 grant: &'a Grant<T, Upcalls, AllowROs, AllowRWs>,
1832
1833 /// Iterator over valid processes.
1834 subiter: core::iter::FilterMap<
1835 core::slice::Iter<'a, ProcessSlot>,
1836 fn(&ProcessSlot) -> Option<&'static dyn Process>,
1837 >,
1838}
1839
1840impl<'a, T: Default, Upcalls: UpcallSize, AllowROs: AllowRoSize, AllowRWs: AllowRwSize> Iterator
1841 for Iter<'a, T, Upcalls, AllowROs, AllowRWs>
1842{
1843 type Item = ProcessGrant<'a, T, Upcalls, AllowROs, AllowRWs>;
1844
1845 fn next(&mut self) -> Option<Self::Item> {
1846 let grant = self.grant;
1847 // Get the next `ProcessId` from the kernel processes array that is
1848 // setup to use this grant. Since the iterator itself is saved calling
1849 // this function again will start where we left off.
1850 self.subiter
1851 .find_map(|process| ProcessGrant::new_if_allocated(grant, process))
1852 }
1853}