7
31
Fork
You've already forked blackmagic
23

Inconsistent jtagtap_cycle() implementations #2180

Closed
opened 2026年01月28日 18:38:39 +01:00 by ALTracer · 1 comment
ALTracer commented 2026年01月28日 18:38:39 +01:00 (Migrated from github.com)
Copy link

A method to generate N cycles of TCK with fixed values of TMS and TDI, jtagtap_cycles(), was added somewhat later than the other base methods to jtag_proc structure of function pointers. I have identified four places where this is used.

  1. jtagtap_init() and equivalents, for emitting SWD LINERESET (50 cycles with TMS high), sometimes coded as a literal for-loop around jtagtap_next() or a prepared byte buffer.
    github.com/blackmagic-debug/blackmagic@54ddc3505c/src/platforms/common/jtagtap.c (L54-L59)
  2. jtag_scan.c:jtag_sanity_check(), called as early as jtag_scan() => jtag_discover() => jtag_sanity_check(). I have touched this recently in #2178.github.com/blackmagic-debug/blackmagic@54ddc3505c/src/target/jtag_scan.c (L306-L307)
  3. adiv5_jtag.c:adiv5_jtag_raw_access(), for 8 idle cycles but only for ADI v6 JTAG-DP targets. Never encountered with ADI v5.github.com/blackmagic-debug/blackmagic@54ddc3505c/src/target/adiv5_jtag.c (L166-L170)
  4. icepick.c:icepick_router_handler(), for 10 idle cycles after reconfiguring type-D controller. Very target-specific.
    github.com/blackmagic-debug/blackmagic@54ddc3505c/src/target/icepick.c (L135-L140)

None of these places are particularly hot code (intensively called during debug). The problem is that BMDA backends for FTDI MPSSE, J-Link, CMSIS-DAP never set the pointer, so it stays NULL (in a calloc()'ed struct), allowing one to inadvertently segfault BMDA mid-scan.

The obvious solutions are a) null-check the pointer at all callsites and fall back to jtagtap_next(); b) code the wrappers which invoke corresponding methods bound to jtagtap_next(). Which can then evolve into proper functions optimized for bit-packing and buffering characteristic for the protocols of these low-level adapters.

I've started at it in #2179 just to fix it until other usable or optimized implementations arrive. The CMSIS-DAP protocol is too convoluted for me to do it properly. J-Link HW_JTAG3 and FTDI MPSSE are easier.

A method to generate N cycles of TCK with fixed values of TMS and TDI, `jtagtap_cycles()`, was added somewhat later than the other base methods to `jtag_proc` structure of function pointers. I have identified four places where this is used. 1. `jtagtap_init()` and equivalents, for emitting SWD LINERESET (50 cycles with TMS high), sometimes coded as a literal for-loop around `jtagtap_next()` or a prepared byte buffer. https://github.com/blackmagic-debug/blackmagic/blob/54ddc3505c5ac3baaa0253ebc08a75444eabf976/src/platforms/common/jtagtap.c#L54-L59 2. jtag_scan.c:`jtag_sanity_check()`, called as early as `jtag_scan()` => `jtag_discover()` => `jtag_sanity_check()`. I have touched this recently in #2178.https://github.com/blackmagic-debug/blackmagic/blob/54ddc3505c5ac3baaa0253ebc08a75444eabf976/src/target/jtag_scan.c#L306-L307 3. adiv5_jtag.c:`adiv5_jtag_raw_access()`, for 8 idle cycles but only for ADI v6 JTAG-DP targets. Never encountered with ADI v5.https://github.com/blackmagic-debug/blackmagic/blob/54ddc3505c5ac3baaa0253ebc08a75444eabf976/src/target/adiv5_jtag.c#L166-L170 4. icepick.c:`icepick_router_handler()`, for 10 idle cycles after reconfiguring type-D controller. Very target-specific. https://github.com/blackmagic-debug/blackmagic/blob/54ddc3505c5ac3baaa0253ebc08a75444eabf976/src/target/icepick.c#L135-L140 None of these places are particularly hot code (intensively called during debug). The problem is that BMDA backends for FTDI MPSSE, J-Link, CMSIS-DAP never set the pointer, so it stays NULL (in a `calloc()`'ed struct), allowing one to inadvertently segfault BMDA mid-scan. The obvious solutions are a) null-check the pointer at all callsites and fall back to `jtagtap_next()`; b) code the wrappers which invoke corresponding methods bound to `jtagtap_next()`. Which can then evolve into proper functions optimized for bit-packing and buffering characteristic for the protocols of these low-level adapters. I've started at it in #2179 just to fix it until other usable or optimized implementations arrive. The CMSIS-DAP protocol is too convoluted for me to do it properly. J-Link HW_JTAG3 and FTDI MPSSE are easier.

Our preference, and what has now been done in #2179 is to provide polyfills over the backend's jtagtap_next() impl to provide the cycles function, and then when a more proper way to implement it is identified for a given backend, replace the polyfill with that specific way, such as building a buffer for FTDI that has the MPSSE run the correct number of cycles in one command with TDI and TMS held in the requested state for all of those cycles.

Because of that PR, we're going to consider this fixed by that now all backends have an implementation.

Our preference, and what has now been done in #2179 is to provide polyfills over the backend's jtagtap_next() impl to provide the cycles function, and then when a more proper way to implement it is identified for a given backend, replace the polyfill with that specific way, such as building a buffer for FTDI that has the MPSSE run the correct number of cycles in one command with TDI and TMS held in the requested state for all of those cycles. Because of that PR, we're going to consider this fixed by that now all backends have an implementation.
Sign in to join this conversation.
No Branch/Tag specified
main
fix/pre-bmp-v3-cross-file-cleanup
feature/bmda-remote-comms
ALTracer/feature/aarch64-ident
feature/better-meson-optimisation-handling
feature/am335x-support
feature/esp32-c3-support
feature/cortex-ar-software-breakpoints
feature/unit-testing
feature/windows-usb-serial-interface-naming
fix/bmp-external-spi
ALTracer/feature/bluepillplus-platform
ALTracer/feature/at32f43x-unrdp
feature/const-correctness
ALTracer/feature/fault_handlers
ALTracer/feature/hazard3-ice40-support
fix/ci-cleanup
ALTracer/fix/gdb-10-12-thread
ALTracer/feature/blackpill-f4-adc
ALTracer/fix/cortex-desc-allocfail-report
ALTracer/feature/spi-perf
ALTracer/feature/calibrate_swd
ALTracer/feature/blank-check
feature/avr
v2.0
v1.9
v1.10
v1.8
v2.1.0-rc1
v2.0.0
v2.0.0-rc2
v2.0.0-rc1
v1.9.3
v1.10.2
v1.10.1
v1.10.0
v1.10.0-rc1
v1.10.0-rc0
v1.9.2
v1.8.3
v1.9.1
v1.9.0
v1.9.0-rc1
v1.9.0-rc0
v1.8.2
v1.8.1
v1.8.0
v1.7.1
v1.7
v1.6.2-rc1
1.6.2-rc0
v1.6.1
v1.6
v1.6-rc1
v1.6-rc0
production_01
production_00
Labels
Clear labels
BMD App
Black Magic Debug App (aka. PC hosted) (not firmware)
BMP Firmware
Black Magic Probe Firmware (not PC hosted software)
Bug
Confirmed bug
Build system
Build system
Can't reproduce
Maintainers can't reproduce this problem
CI
Continuous Integration System
Contribution wanted
User contributions welcome
Documentation
Project documentation
Draft
Work in progress draft
Duplicate
This issue or pull request already exists
Enhancement
General project improvement
Feedback wanted
Requires additional submitter feedback
Foreign Host Board
Non Native hardware to runing Black Magic firmware on
GDB
Issue/PR related to GDB
Good first issue
Good for newcommers
HwIssue Mitigation
Solving or mitigating a Hardware issue in Software
Information Needed
Maintainers need more information
NativeHardware
Official Black Magic Debug Hardware
New Host Board
New hardware to run Black Magic firmware on
New Target
New debug target
Off Topic
Something that does not involve the project in any way
Potential Bug
A potential, unconfirmed or very special circumstance bug
Regression
Bug caused by a regression
User Interest Needed
More user interest required before consideration
User Testing Needed
Looking for user testing reports
Won't fix
Outside of the project scope or works as intended
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
blackmagic-debug/blackmagic#2180
Reference in a new issue
blackmagic-debug/blackmagic
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?