Merge branch 'main' into tts

This commit is contained in:
bunnie 2022-03-10 02:22:05 +08:00
commit a998cf06c4
7 changed files with 1412 additions and 9833 deletions

View file

@ -354,6 +354,27 @@ def main():
else:
patchlen += 1
# test to see if we can emit an optimized version of the code
is_wellformed = True
for frame_rec in patchdata_sorted:
frame = frame_rec[1]
words_used = [0] * 101
for word in range(101):
if frame[word] == None:
continue
words_used[word] = 1
if words_used[:32] != [1] * 32:
is_wellformed = False
if words_used[32:] != [0] * (101-32):
is_wellformed = False
if is_wellformed:
print("Patch list is well formed, generating optimized code")
generate_optimized(f, patchdata_sorted, patchlen)
exit(0)
else:
print("Patch list is irregular, generating general-case code (is_wellformed: {})".format(is_wellformed))
f.write("""
//! this file was auto-generated by key2bits.py on {}
//! manual regeneration is needed for the following cases:
@ -472,7 +493,9 @@ pub fn should_patch(frame: u32) -> bool {{
}}
}}
""".format(min(frame_nos), max(frame_nos)))
generate_test(f, patchdata_sorted)
def generate_test(f, patchdata_sorted):
f.write("""
#[cfg(test)]
mod tests {
@ -501,14 +524,164 @@ mod tests {
bitvalue = (keyrom[coord[0]] & (1 << coord[1]))
if bitvalue != 0:
wordvalue |= (1 << bit)
f.write(' assert_eq!(crate::patch_frame({}'.format(frame_rec[0]) + ', ' + '{}'.format(word) + ', &ROM), (Some(' + '0x{:08x}'.format(wordvalue) + '), Some(!0x{:08x}'.format(wordvalue) + ')));\n')
f.write(' assert_eq!(crate::key2bits::patch_frame({}'.format(frame_rec[0]) + ', ' + '{}'.format(word) + ', &ROM), Some((' + '0x{:08x}u32'.format(wordvalue) + ', !0x{:08x}u32'.format(wordvalue) + ')));\n')
f.write(' // also test the null case, frame 0 should typically have no mappings.\n')
f.write(' assert_eq!(crate::patch_frame(0x0, 0, &ROM), (None, None) );\n')
f.write(' assert_eq!(crate::key2bits::patch_frame(0x0, 0, &ROM), None );\n')
f.write("""
}
}
""")
def generate_optimized(f, patchdata_sorted, patchlen):
sequences = []
sequence = []
prev_frame = None
for frame_rec in patchdata_sorted:
if prev_frame == None:
prev_frame = frame_rec[0]
sequence.append(frame_rec)
else:
if frame_rec[0] - prev_frame != 1:
sequences.append(sequence)
sequence = [frame_rec]
else:
sequence.append(frame_rec)
prev_frame = frame_rec[0]
sequences.append(sequence)
# import pprint
# pprint.pprint(sequences, indent=6)
f.write("""
//! this file was auto-generated by key2bits.py on {}
//! manual regeneration is needed for the following cases:
//! - rom.db changes (that is, the KEYROM pcells have been moved to a new location)
//! - entirely different FPGA target (in which case, the db/ files also need to be regenerated
//! (this is a long process if you're not targting a prjxray pre-build target))
//! - bug fixes and enhancements to these routines
""".format(str(datetime.now())))
f.write("""
pub const PATCH_FRAMES: [u32; {}] = [\n""".format(len(patchdata_sorted)))
for frame_rec in patchdata_sorted:
f.write(" 0x{:x},\n".format(frame_rec[0]))
f.write("];\n")
for sequence in sequences:
f.write("""
const PATCH_TABLE_{}: [u8; {}] = [\n""".format(sequence[0][0], len(sequence) * 2 * 32 * 32))
for frame_rec in sequence:
f.write(" // frame 0x{:x} ({})".format(frame_rec[0], frame_rec[0]))
for bitdict in frame_rec[1]:
if bitdict == None:
continue
for index in range(32):
if (index % 16 == 0):
f.write("\n ")
f.write("{:03},{:03},".format(bitdict[index][0], bitdict[index][1]))
f.write("\n")
f.write("];\n")
f.write("""
/// patch a frame at a given relative positition and offset in the framestream
/// to insert a key ROM.
///
/// frame is the frame number in the framestream
/// offset is the offset in the frame, from 0-100 (101 words)
/// note that each primitive in a bitstream is a u32
///
/// This code path has been optimized because the patch set is well-formed, such that
/// the patches are all located in frames 0-31, and are mostly sequential in frames.
/// This allows us to use direct array accessors with index operations, instead of a
/// more general data structure. This method is less readable, but saves a significant
/// amount of code space and reduces compile times. Refer to the generation script
/// if a more general form is desired; this option is retained for reference purposes.
///
/// returns None if the position should not be patched
/// returns a tuple of the value and its inverse; this is done to reduce the timing
/// sidechannel. The inverse value needs to be consumed to prevent the compiler
/// from optimizing out that path.
""")
f.write("""
/// a fast check to see if a given frame is within the range of frames that are
/// patchable. Use this to wrap calls to `patch_frame` as a performance optimization.
pub fn should_patch(frame: u32) -> bool {
""")
f.write(" if")
first_element = True
for sequence in sequences:
if first_element:
f.write(" (frame >= 0x{:x} && frame <= 0x{:x})\n".format(sequence[0][0], sequence[-1][0]))
first_element = False
else:
f.write(" || (frame >= 0x{:x} && frame <= 0x{:x})\n".format(sequence[0][0], sequence[-1][0]))
f.write(""" {
true
} else {
false
}
}""")
f.write("""
fn get_patch_subtable(frame: u32, offset: u32, table_base: u32) -> &'static [u8] {
// ASSUME: all offsets are checked before here. Worst case, we throw a panic because of an OOB index.
// A frame table consists of:
// - 32 patchable words in a frame (it is a convenient coincidence that the
// patchable words are at positions 0-31: if the ROM LUTs were implemented
// differently, this may not be the case)
// - Each word to patch is 32 bits long, thus requiring as many entries
// - Each entry consisting of 2 items:
// - A u8 address in the conventionally-ordered source key ROM
// - A u8 bit position of the data at that address in the same key ROM
// Thus a patch table says for a given frame and offset, you may replace the
// values with those found at the given address and bit offsets in a key ROM
// to program that key ROM into the LUT
let base = ((frame - table_base) * 2 * 32 * 32 + (offset * 2 * 32)) as usize;
match table_base {
""")
for sequence in sequences:
f.write(" {} => &PATCH_TABLE_{}[base..base + 2 * 32],\n".format(sequence[0][0], sequence[0][0]))
f.write(""" _ => panic!("invalid table base in patch.rs")
}
}
""")
f.write("""
pub fn patch_frame(frame: u32, offset: u32, rom: &[u32]) -> Option<(u32, u32)> {
if offset >= 32 {
return None;
}
if should_patch(frame) {
// ASSUME: `frame` meets the requirements outlined above
let table_base = match frame {
""")
for sequence in sequences:
base = sequence[0][0]
for run in sequence:
f.write(" {} => {},\n".format(run[0], base))
f.write(""" _ => panic!("invalid frame in patch.rs"),
};
let subtable = get_patch_subtable(frame, offset, table_base);
let mut data: u32 = 0;
let mut data_inv: u32 = 0;
for bit in 0..32 {
let (adr, patch_bit) = (subtable[bit * 2], subtable[bit * 2 + 1]);
let romval: u32 = rom[adr as usize] & (1 << patch_bit);
if romval != 0 {
data |= 1 << bit;
} else {
data_inv |= 1 << bit;
}
}
Some((data, data_inv))
} else {
None
}
}
""")
generate_test(f, patchdata_sorted)
if __name__ == "__main__":
main()

File diff suppressed because it is too large Load diff

View file

@ -377,8 +377,6 @@ mod bcrypt;
// some short tests to just confirm we're not totally broken.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hash_with_fixed_salt() {
let salt: [u8; 16] = [

View file

@ -15,8 +15,14 @@ pub(crate) enum Opcode {
/// force a WDT update
PingWdt = 4,
/// return the version string of Xous. We bury it here because this is a small, lightweight server we can rebuild on every run.
/// Return the version string of Xous. We bury it here because this is a small, lightweight server we can rebuild on every run.
GetVersion = 5,
/// Lock the given Mutex, blocking if it is already locked
LockMutex = 6,
/// Unlock the given Mutex
UnlockMutex = 7,
}
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]

View file

@ -66,11 +66,61 @@ impl Ticktimer {
version: xous_ipc::String::new(),
};
let mut buf = xous_ipc::Buffer::into_buf(alloc).expect("couldn't convert version request");
buf.lend_mut(self.conn, api::Opcode::GetVersion.to_u32().unwrap()).expect("couldn't get version");
let v = buf.to_original::<api::VersionString, _>().expect("couldn't revert buffer");
buf.lend_mut(self.conn, api::Opcode::GetVersion.to_u32().unwrap())
.expect("couldn't get version");
let v = buf
.to_original::<api::VersionString, _>()
.expect("couldn't revert buffer");
String::from(v.version.as_str().unwrap())
}
/// Lock the given Mutex. Blocks until the Mutex is locked.
///
/// Note that Mutexes start out in a `Locked` state and move into an `Unlocked` state by calling
/// `Unlock` on their pointer. For example, the following will probably block forever:
///
/// `TickTimer.lock_mutex(1)`
///
/// In order to create a new Mutex, you must first `Unlock` it. For example, the following is
/// allowed:
///
/// `TickTimer.unlock_mutex(1)`
/// `TickTimer.lock_mutex(1)`
/// `TickTimer.unlock_mutex(1)`
///
/// Arguments:
///
/// * mtx: A `usize` referring to the Mutex. This is probably a pointer, but can be any `usize`
pub fn lock_mutex(&self, mtx: usize) {
send_message(
self.conn,
xous::Message::new_blocking_scalar(
api::Opcode::LockMutex.to_usize().unwrap(),
mtx,
0,
0,
0,
),
)
.expect("couldn't lock mutex");
}
/// Unlock the given Mutex. Does not block. If the Mutex is not locked, then it will be
/// "doubly-unlocked". That is, if you Unlock a mutex twice, then you can Lock it twice
/// without blocking.
///
/// Arguments:
///
/// * mtx: A `usize` referring to the Mutex. This is probably a pointer, but can be any `usize`
pub fn unlock_mutex(&self, mtx: usize) {
send_message(
self.conn,
xous::Message::new_scalar(api::Opcode::UnlockMutex.to_usize().unwrap(), mtx, 0, 0, 0),
)
.expect("couldn't lock mutex");
}
}
use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
impl Drop for Ticktimer {

View file

@ -475,7 +475,7 @@ fn xmain() -> ! {
log::info!("****************************************************************");
log::info!("Welcome to Xous {}", version::SEMVER);
#[cfg(not(feature="no-timestamp"))]
#[cfg(not(feature = "no-timestamp"))]
log::info!("Built on {}", version::TIMESTAMP);
log::info!("****************************************************************");
@ -495,8 +495,27 @@ fn xmain() -> ! {
let xns = xous_names::XousNames::new().unwrap();
let sr_cid =
xous::connect(ticktimer_server).expect("couldn't create suspend callback connection");
let mut susres = susres::Susres::new(Some(SuspendOrder::Last), &xns, api::Opcode::SuspendResume as u32, sr_cid)
.expect("couldn't create suspend/resume object");
let mut susres = susres::Susres::new(
Some(SuspendOrder::Last),
&xns,
api::Opcode::SuspendResume as u32,
sr_cid,
)
.expect("couldn't create suspend/resume object");
use std::collections::{HashMap, HashSet, VecDeque};
// A list of mutexes that should be allowed to run immediately, because the
// thread they were waiting on has already unlocked the mutex. This occurs
// when a thread attempts to Lock a Mutex and fails, then gets preempted
// before it has a chance to send the `LockMutex` message to us.
let mut mutex_ready_hash: HashMap<Option<xous::PID>, HashSet<usize>> = HashMap::new();
// A list of message IDs that are waiting to lock a Mutex. These are processes
// that have attempted to lock a Mutex and failed, and have sent us the `LockMutex`
// message. This queue is drained by threads sending `UnlockMutex` to us.
let mut mutex_hash: HashMap<Option<xous::PID>, HashMap<usize, VecDeque<xous::MessageSender>>> =
HashMap::new();
loop {
#[cfg(feature = "watchdog")]
@ -540,9 +559,60 @@ fn xmain() -> ! {
ticktimer.reset_wdt();
}
Some(api::Opcode::GetVersion) => {
let mut buf = unsafe{xous_ipc::Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())};
let mut buf = unsafe {
xous_ipc::Buffer::from_memory_message_mut(
msg.body.memory_message_mut().unwrap(),
)
};
buf.replace(version::get_version()).unwrap();
}
Some(api::Opcode::LockMutex) => {
let pid = msg.sender.pid();
if !msg.body.is_blocking() {
info!("sender made LockMutex request that was not blocking");
break;
}
if let Some(scalar) = msg.body.scalar_message() {
let ready = mutex_ready_hash.entry(pid).or_default();
// If this item is in the Ready list, return right away without blocking
if ready.remove(&scalar.arg1) {
xous::return_scalar(msg.sender, 0).unwrap();
break;
}
// This item is not in the Ready list, so add our sender to the list of processes
// to get called when UnlockMutex is invoked
let awaiting = mutex_hash.entry(pid).or_default();
// Add this to the end of the list of entries to call so that when `UnlockMutex` is sent
// the message will get a response.
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
mutex_entry.push_back(msg.sender);
}
}
Some(api::Opcode::UnlockMutex) => {
let pid = msg.sender.pid();
if msg.body.is_blocking() {
info!("sender made UnlockMutex request that was blocking");
break;
}
if let Some(scalar) = msg.body.scalar_message() {
// Get a list of awaiting mutexes for this process
let awaiting = mutex_hash.entry(pid).or_default();
// Get the vector of awaiting mutex entries.
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
// If there's something waiting in the queue, respond to that message
if let Some(sender) = mutex_entry.pop_front() {
xous::return_scalar(sender, 0).unwrap();
} else {
// Otherwise, mark this scalar as being ready to run
mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1);
}
}
}
None => {
error!("couldn't convert opcode");
break;

View file

@ -57,7 +57,7 @@ pub static TESTING_RNG_SEED: AtomicU64 = AtomicU64::new(0);
pub mod exceptions;
pub use exceptions::*;
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Default)]
pub struct MessageSender {
data: usize,
}
@ -77,12 +77,6 @@ impl MessageSender {
}
}
impl core::default::Default for MessageSender {
fn default() -> Self {
MessageSender { data: 0 }
}
}
impl core::fmt::Display for MessageSender {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "MessageSender.data: 0x{:08x}", self.data)