Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70d9adb

Browse files
committed
capsules/sdcard: migrate non-virtualized userspace driver to Grants
As part of tock#2462, this migrates the sdcard non-virtualized userspace driver to keep process state in a Grant region. It adopts the mechanism to reserve a driver as introduced in tock#2521. Signed-off-by: Leon Schuermann <[email protected]>
1 parent f769dc5 commit 70d9adb

File tree

1 file changed

+150
-88
lines changed

1 file changed

+150
-88
lines changed

capsules/src/sdcard.rs

Lines changed: 150 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ use core::cell::Cell;
4444
use core::cmp;
4545
use core::mem;
4646

47-
use kernel::common::cells::{MapCell, OptionalCell, TakeCell};
47+
use kernel::common::cells::{OptionalCell, TakeCell};
4848
use kernel::hil;
4949
use kernel::ErrorCode;
50-
use kernel::{CommandReturn, Driver, ProcessId, Upcall};
50+
use kernel::{CommandReturn, Driver, Grant, ProcessId, Upcall};
5151
use kernel::{Read, ReadOnlyAppSlice, ReadWrite, ReadWriteAppSlice};
5252

5353
/// Syscall driver number.
@@ -1405,13 +1405,14 @@ impl<'a, A: hil::time::Alarm<'a>> hil::gpio::Client for SDCard<'a, A> {
14051405
/// off of the SDCard instead
14061406
pub struct SDCardDriver<'a, A: hil::time::Alarm<'a>> {
14071407
sdcard: &'a SDCard<'a, A>,
1408-
app: MapCell<App>,
14091408
kernel_buf: TakeCell<'static, [u8]>,
1409+
grants: Grant<App>,
1410+
current_process: OptionalCell<ProcessId>,
14101411
}
14111412

14121413
/// Holds buffers and whatnot that the application has passed us.
14131414
#[derive(Default)]
1414-
struct App {
1415+
pub struct App {
14151416
callback: Upcall,
14161417
write_buffer: ReadOnlyAppSlice,
14171418
read_buffer: ReadWriteAppSlice,
@@ -1430,68 +1431,82 @@ impl<'a, A: hil::time::Alarm<'a>> SDCardDriver<'a, A> {
14301431
pub fn new(
14311432
sdcard: &'a SDCard<'a, A>,
14321433
kernel_buf: &'static mut [u8; 512],
1434+
grants: Grant<App>,
14331435
) -> SDCardDriver<'a, A> {
14341436
// return new SDCardDriver
14351437
SDCardDriver {
1436-
sdcard: sdcard,
1437-
app: MapCell::new(App::default()),
1438+
sdcard,
14381439
kernel_buf: TakeCell::new(kernel_buf),
1440+
grants,
1441+
current_process: OptionalCell::empty(),
14391442
}
14401443
}
14411444
}
14421445

14431446
/// Handle callbacks from SDCard
14441447
impl<'a, A: hil::time::Alarm<'a>> SDCardClient for SDCardDriver<'a, A> {
14451448
fn card_detection_changed(&self, installed: bool) {
1446-
self.app.map(|app| {
1447-
app.callback.schedule(0, installed as usize, 0);
1449+
self.current_process.map(|process_id| {
1450+
let _ = self.grants.enter(*process_id, |app| {
1451+
app.callback.schedule(0, installed as usize, 0);
1452+
});
14481453
});
14491454
}
14501455

14511456
fn init_done(&self, block_size: u32, total_size: u64) {
1452-
self.app.map(|app| {
1453-
let size_in_kb = ((total_size >> 10) & 0xFFFFFFFF) as usize;
1454-
app.callback.schedule(1, block_size as usize, size_in_kb);
1457+
self.current_process.map(|process_id| {
1458+
let _ = self.grants.enter(*process_id, |app| {
1459+
let size_in_kb = ((total_size >> 10) & 0xFFFFFFFF) as usize;
1460+
app.callback.schedule(1, block_size as usize, size_in_kb);
1461+
});
14551462
});
14561463
}
14571464

14581465
fn read_done(&self, data: &'static mut [u8], len: usize) {
14591466
self.kernel_buf.replace(data);
1460-
self.app.map(|app| {
1461-
let mut read_len = 0;
1462-
self.kernel_buf.map(|data| {
1463-
app.read_buffer.mut_map_or(0, |read_buffer| {
1464-
let read_buffer = read_buffer;
1465-
// copy bytes to user buffer
1466-
// Limit to minimum length between read_buffer, data, and
1467-
// len field
1468-
for (read_byte, &data_byte) in read_buffer.iter_mut().zip(data.iter()).take(len)
1469-
{
1470-
*read_byte = data_byte;
1471-
}
1472-
read_len = cmp::min(read_buffer.len(), cmp::min(data.len(), len));
1473-
read_len
1467+
1468+
self.current_process.map(|process_id| {
1469+
let _ = self.grants.enter(*process_id, |app| {
1470+
let mut read_len = 0;
1471+
self.kernel_buf.map(|data| {
1472+
app.read_buffer.mut_map_or(0, |read_buffer| {
1473+
let read_buffer = read_buffer;
1474+
// copy bytes to user buffer
1475+
// Limit to minimum length between read_buffer, data, and
1476+
// len field
1477+
for (read_byte, &data_byte) in
1478+
read_buffer.iter_mut().zip(data.iter()).take(len)
1479+
{
1480+
*read_byte = data_byte;
1481+
}
1482+
read_len = cmp::min(read_buffer.len(), cmp::min(data.len(), len));
1483+
read_len
1484+
});
14741485
});
1475-
});
14761486

1477-
// perform callback
1478-
// Note that we are explicitly performing the callback even if no
1479-
// data was read or if the app's read_buffer doesn't exist
1480-
app.callback.schedule(2, read_len, 0);
1487+
// perform callback
1488+
// Note that we are explicitly performing the callback even if no
1489+
// data was read or if the app's read_buffer doesn't exist
1490+
app.callback.schedule(2, read_len, 0);
1491+
});
14811492
});
14821493
}
14831494

14841495
fn write_done(&self, buffer: &'static mut [u8]) {
14851496
self.kernel_buf.replace(buffer);
14861497

1487-
self.app.map(|app| {
1488-
app.callback.schedule(3, 0, 0);
1498+
self.current_process.map(|process_id| {
1499+
let _ = self.grants.enter(*process_id, |app| {
1500+
app.callback.schedule(3, 0, 0);
1501+
});
14891502
});
14901503
}
14911504

14921505
fn error(&self, error: u32) {
1493-
self.app.map(|app| {
1494-
app.callback.schedule(4, error as usize, 0);
1506+
self.current_process.map(|process_id| {
1507+
let _ = self.grants.enter(*process_id, |app| {
1508+
app.callback.schedule(4, error as usize, 0);
1509+
});
14951510
});
14961511
}
14971512
}
@@ -1500,63 +1515,107 @@ impl<'a, A: hil::time::Alarm<'a>> SDCardClient for SDCardDriver<'a, A> {
15001515
impl<'a, A: hil::time::Alarm<'a>> Driver for SDCardDriver<'a, A> {
15011516
fn allow_readwrite(
15021517
&self,
1503-
_appid: ProcessId,
1518+
process_id: ProcessId,
15041519
allow_num: usize,
15051520
mut slice: ReadWriteAppSlice,
15061521
) -> Result<ReadWriteAppSlice, (ReadWriteAppSlice, ErrorCode)> {
1507-
match allow_num {
1508-
// Pass read buffer in from application
1509-
0 => {
1510-
self.app.map(|app| {
1511-
mem::swap(&mut app.read_buffer, &mut slice);
1512-
});
1513-
Ok(slice)
1514-
}
1515-
_ => Err((slice, ErrorCode::NOSUPPORT)),
1522+
let res = self
1523+
.grants
1524+
.enter(process_id, |grant| {
1525+
match allow_num {
1526+
// Pass read buffer in from application
1527+
0 => {
1528+
mem::swap(&mut grant.read_buffer, &mut slice);
1529+
Ok(())
1530+
}
1531+
_ => Err(ErrorCode::NOSUPPORT),
1532+
}
1533+
})
1534+
.unwrap_or_else(|e| e.into());
1535+
1536+
match res {
1537+
Ok(()) => Ok(slice),
1538+
Err(e) => Err((slice, e)),
15161539
}
15171540
}
15181541

15191542
fn allow_readonly(
15201543
&self,
1521-
_appid: ProcessId,
1544+
process_id: ProcessId,
15221545
allow_num: usize,
15231546
mut slice: ReadOnlyAppSlice,
15241547
) -> Result<ReadOnlyAppSlice, (ReadOnlyAppSlice, ErrorCode)> {
1525-
// Pass write buffer in from application
1526-
match allow_num {
1527-
0 => {
1528-
self.app.map(|app| {
1529-
mem::swap(&mut app.write_buffer, &mut slice);
1530-
});
1531-
Ok(slice)
1532-
}
1533-
_ => Err((slice, ErrorCode::NOSUPPORT)),
1548+
let res = self
1549+
.grants
1550+
.enter(process_id, |grant| {
1551+
match allow_num {
1552+
// Pass write buffer in from application
1553+
0 => {
1554+
mem::swap(&mut grant.write_buffer, &mut slice);
1555+
Ok(())
1556+
}
1557+
_ => Err(ErrorCode::NOSUPPORT),
1558+
}
1559+
})
1560+
.unwrap_or_else(|e| e.into());
1561+
1562+
match res {
1563+
Ok(()) => Ok(slice),
1564+
Err(e) => Err((slice, e)),
15341565
}
15351566
}
15361567

15371568
fn subscribe(
15381569
&self,
15391570
subscribe_num: usize,
15401571
mut callback: Upcall,
1541-
_app_id: ProcessId,
1572+
process_id: ProcessId,
15421573
) -> Result<Upcall, (Upcall, ErrorCode)> {
1543-
match subscribe_num {
1544-
// Set callback
1545-
0 => {
1546-
self.app.map(|app| {
1547-
mem::swap(&mut app.callback, &mut callback);
1548-
});
1549-
Ok(callback)
1550-
}
1551-
_ => Err((callback, ErrorCode::NOSUPPORT)),
1574+
let res = self
1575+
.grants
1576+
.enter(process_id, |grant| {
1577+
match subscribe_num {
1578+
// Set callback
1579+
0 => {
1580+
mem::swap(&mut grant.callback, &mut callback);
1581+
Ok(())
1582+
}
1583+
_ => Err(ErrorCode::NOSUPPORT),
1584+
}
1585+
})
1586+
.unwrap_or_else(|e| e.into());
1587+
1588+
match res {
1589+
Ok(()) => Ok(callback),
1590+
Err(e) => Err((callback, e)),
15521591
}
15531592
}
15541593

1555-
fn command(&self, command_num: usize, data: usize, _: usize, _: ProcessId) -> CommandReturn {
1556-
match command_num {
1557-
// check if present
1558-
0 => CommandReturn::success(),
1594+
fn command(
1595+
&self,
1596+
command_num: usize,
1597+
data: usize,
1598+
_: usize,
1599+
process_id: ProcessId,
1600+
) -> CommandReturn {
1601+
if command_num == 0 {
1602+
// Handle this first as it should be returned unconditionally.
1603+
return CommandReturn::success();
1604+
}
1605+
1606+
// Check if this driver is free, or already dedicated to this process.
1607+
let match_or_empty_or_nonexistant = self.current_process.map_or(true, |current_process| {
1608+
self.grants
1609+
.enter(*current_process, |_| current_process == &process_id)
1610+
.unwrap_or(true)
1611+
});
1612+
if match_or_empty_or_nonexistant {
1613+
self.current_process.set(process_id);
1614+
} else {
1615+
return CommandReturn::failure(ErrorCode::NOMEM);
1616+
}
15591617

1618+
match command_num {
15601619
// is_installed
15611620
1 => {
15621621
let value = self.sdcard.is_installed() as u32;
@@ -1579,26 +1638,29 @@ impl<'a, A: hil::time::Alarm<'a>> Driver for SDCardDriver<'a, A> {
15791638

15801639
// write_block
15811640
4 => {
1582-
let result: Result<(), ErrorCode> = self.app.map_or(Err(ErrorCode::NOMEM), |app| {
1583-
app.write_buffer
1584-
.map_or(Err(ErrorCode::NOMEM), |write_buffer| {
1585-
self.kernel_buf
1586-
.take()
1587-
.map_or(Err(ErrorCode::BUSY), |kernel_buf| {
1588-
// copy over write data from application
1589-
// Limit to minimum length between kernel_buf,
1590-
// write_buffer, and 512 (block size)
1591-
for (kernel_byte, &write_byte) in
1592-
kernel_buf.iter_mut().zip(write_buffer.iter()).take(512)
1593-
{
1594-
*kernel_byte = write_byte;
1595-
}
1596-
1597-
// begin writing
1598-
self.sdcard.write_blocks(kernel_buf, data as u32, 1)
1599-
})
1600-
})
1601-
});
1641+
let result: Result<(), ErrorCode> = self
1642+
.grants
1643+
.enter(process_id, |app| {
1644+
app.write_buffer
1645+
.map_or(Err(ErrorCode::NOMEM), |write_buffer| {
1646+
self.kernel_buf
1647+
.take()
1648+
.map_or(Err(ErrorCode::BUSY), |kernel_buf| {
1649+
// copy over write data from application
1650+
// Limit to minimum length between kernel_buf,
1651+
// write_buffer, and 512 (block size)
1652+
for (kernel_byte, &write_byte) in
1653+
kernel_buf.iter_mut().zip(write_buffer.iter()).take(512)
1654+
{
1655+
*kernel_byte = write_byte;
1656+
}
1657+
1658+
// begin writing
1659+
self.sdcard.write_blocks(kernel_buf, data as u32, 1)
1660+
})
1661+
})
1662+
})
1663+
.unwrap_or(Err(ErrorCode::NOMEM));
16021664
CommandReturn::from(result)
16031665
}
16041666

0 commit comments

Comments
 (0)