mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-01 12:22:34 +00:00
perf(core): share wire buffer between wire and debuglink
[no changelog]
This commit is contained in:
parent
582e1318c4
commit
8da978981e
@ -130,8 +130,6 @@ DUMMY_CONTEXT = DummyContext()
|
|||||||
PROTOBUF_BUFFER_SIZE = 8192
|
PROTOBUF_BUFFER_SIZE = 8192
|
||||||
|
|
||||||
WIRE_BUFFER = bytearray(PROTOBUF_BUFFER_SIZE)
|
WIRE_BUFFER = bytearray(PROTOBUF_BUFFER_SIZE)
|
||||||
if __debug__:
|
|
||||||
WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE)
|
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
@ -378,11 +376,7 @@ async def _handle_single_message(
|
|||||||
async def handle_session(
|
async def handle_session(
|
||||||
iface: WireInterface, session_id: int, is_debug_session: bool = False
|
iface: WireInterface, session_id: int, is_debug_session: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
if __debug__ and is_debug_session:
|
ctx = Context(iface, session_id, WIRE_BUFFER)
|
||||||
ctx_buffer = WIRE_BUFFER_DEBUG
|
|
||||||
else:
|
|
||||||
ctx_buffer = WIRE_BUFFER
|
|
||||||
ctx = Context(iface, session_id, ctx_buffer)
|
|
||||||
next_msg: codec_v1.Message | None = None
|
next_msg: codec_v1.Message | None = None
|
||||||
|
|
||||||
if __debug__ and is_debug_session:
|
if __debug__ and is_debug_session:
|
||||||
|
@ -4,6 +4,8 @@ from micropython import const
|
|||||||
from trezor import io, loop, utils
|
from trezor import io, loop, utils
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from trezorio import WireInterface
|
from trezorio import WireInterface
|
||||||
|
|
||||||
_REP_LEN = const(64)
|
_REP_LEN = const(64)
|
||||||
@ -17,6 +19,35 @@ _REP_CONT_DATA = const(1) # offset of data in the continuation report
|
|||||||
SESSION_ID = const(0)
|
SESSION_ID = const(0)
|
||||||
INVALID_TYPE = const(-1)
|
INVALID_TYPE = const(-1)
|
||||||
|
|
||||||
|
# The wire buffer is shared between the main wire interface and debuglink
|
||||||
|
# (see __init__.py). There's no obvious guarantee that both interfaces won't
|
||||||
|
# use it at the same time, thus we check this at runtime in debug builds.
|
||||||
|
if __debug__:
|
||||||
|
|
||||||
|
class BufferLock(object):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.in_use = False
|
||||||
|
|
||||||
|
def __enter__(self) -> None:
|
||||||
|
assert not self.in_use, "global buffer already used by another context"
|
||||||
|
self.in_use = True
|
||||||
|
|
||||||
|
def __exit__(self, type: Any, value: Any, traceback: Any) -> None:
|
||||||
|
self.in_use = False
|
||||||
|
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
class BufferLock(object): # type: ignore
|
||||||
|
def __enter__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self, type: Any, value: Any, traceback: Any) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
buffer_lock = BufferLock()
|
||||||
|
|
||||||
|
|
||||||
class CodecError(Exception):
|
class CodecError(Exception):
|
||||||
pass
|
pass
|
||||||
@ -41,31 +72,32 @@ async def read_message(iface: WireInterface, buffer: utils.BufferType) -> Messag
|
|||||||
|
|
||||||
read_and_throw_away = False
|
read_and_throw_away = False
|
||||||
|
|
||||||
if msize > len(buffer):
|
with buffer_lock:
|
||||||
# allocate a new buffer to fit the message
|
if msize > len(buffer):
|
||||||
try:
|
# allocate a new buffer to fit the message
|
||||||
mdata: utils.BufferType = bytearray(msize)
|
try:
|
||||||
except MemoryError:
|
mdata: utils.BufferType = bytearray(msize)
|
||||||
mdata = bytearray(_REP_LEN)
|
except MemoryError:
|
||||||
read_and_throw_away = True
|
mdata = bytearray(_REP_LEN)
|
||||||
else:
|
read_and_throw_away = True
|
||||||
# reuse a part of the supplied buffer
|
|
||||||
mdata = memoryview(buffer)[:msize]
|
|
||||||
|
|
||||||
# buffer the initial data
|
|
||||||
nread = utils.memcpy(mdata, 0, report, _REP_INIT_DATA)
|
|
||||||
|
|
||||||
while nread < msize:
|
|
||||||
# wait for continuation report
|
|
||||||
report = await read
|
|
||||||
if report[0] != _REP_MARKER:
|
|
||||||
raise CodecError("Invalid magic")
|
|
||||||
|
|
||||||
# buffer the continuation data
|
|
||||||
if read_and_throw_away:
|
|
||||||
nread += len(report) - 1
|
|
||||||
else:
|
else:
|
||||||
nread += utils.memcpy(mdata, nread, report, _REP_CONT_DATA)
|
# reuse a part of the supplied buffer
|
||||||
|
mdata = memoryview(buffer)[:msize]
|
||||||
|
|
||||||
|
# buffer the initial data
|
||||||
|
nread = utils.memcpy(mdata, 0, report, _REP_INIT_DATA)
|
||||||
|
|
||||||
|
while nread < msize:
|
||||||
|
# wait for continuation report
|
||||||
|
report = await read
|
||||||
|
if report[0] != _REP_MARKER:
|
||||||
|
raise CodecError("Invalid magic")
|
||||||
|
|
||||||
|
# buffer the continuation data
|
||||||
|
if read_and_throw_away:
|
||||||
|
nread += len(report) - 1
|
||||||
|
else:
|
||||||
|
nread += utils.memcpy(mdata, nread, report, _REP_CONT_DATA)
|
||||||
|
|
||||||
if read_and_throw_away:
|
if read_and_throw_away:
|
||||||
raise CodecError("Message too large")
|
raise CodecError("Message too large")
|
||||||
@ -76,30 +108,31 @@ async def read_message(iface: WireInterface, buffer: utils.BufferType) -> Messag
|
|||||||
async def write_message(iface: WireInterface, mtype: int, mdata: bytes) -> None:
|
async def write_message(iface: WireInterface, mtype: int, mdata: bytes) -> None:
|
||||||
write = loop.wait(iface.iface_num() | io.POLL_WRITE)
|
write = loop.wait(iface.iface_num() | io.POLL_WRITE)
|
||||||
|
|
||||||
# gather data from msg
|
with buffer_lock:
|
||||||
msize = len(mdata)
|
# gather data from msg
|
||||||
|
msize = len(mdata)
|
||||||
|
|
||||||
# prepare the report buffer with header data
|
# prepare the report buffer with header data
|
||||||
report = bytearray(_REP_LEN)
|
report = bytearray(_REP_LEN)
|
||||||
repofs = _REP_INIT_DATA
|
repofs = _REP_INIT_DATA
|
||||||
ustruct.pack_into(
|
ustruct.pack_into(
|
||||||
_REP_INIT, report, 0, _REP_MARKER, _REP_MAGIC, _REP_MAGIC, mtype, msize
|
_REP_INIT, report, 0, _REP_MARKER, _REP_MAGIC, _REP_MAGIC, mtype, msize
|
||||||
)
|
)
|
||||||
|
|
||||||
nwritten = 0
|
nwritten = 0
|
||||||
while True:
|
|
||||||
# copy as much as possible to the report buffer
|
|
||||||
nwritten += utils.memcpy(report, repofs, mdata, nwritten)
|
|
||||||
|
|
||||||
# write the report
|
|
||||||
while True:
|
while True:
|
||||||
await write
|
# copy as much as possible to the report buffer
|
||||||
n = iface.write(report)
|
nwritten += utils.memcpy(report, repofs, mdata, nwritten)
|
||||||
if n == len(report):
|
|
||||||
break
|
|
||||||
|
|
||||||
# if we have more data to write, use continuation reports for it
|
# write the report
|
||||||
if nwritten < msize:
|
while True:
|
||||||
repofs = _REP_CONT_DATA
|
await write
|
||||||
else:
|
n = iface.write(report)
|
||||||
break
|
if n == len(report):
|
||||||
|
break
|
||||||
|
|
||||||
|
# if we have more data to write, use continuation reports for it
|
||||||
|
if nwritten < msize:
|
||||||
|
repofs = _REP_CONT_DATA
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user