Skip to content

amiga: fix 68000 address errors; tune the 68000 builds for the 68000 - #62

Merged
reinauer merged 2 commits into
mainfrom
m68000-unaligned
Nov 28, 2026
Merged

reinauer merged 2 commits into
mainfrom
m68000-unaligned

Conversation

@reinauer

@reinauer reinauer commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Fixes #61.

What was wrong

GCC's __mc680x0__ macros follow -mtune, not -m: with -m68000 -mtune=68020-60 it defines __mc68020__ through __mc68060__, and the full set of predefined macros is identical to -m68020 -mtune=68020-60. No compiler macro can tell the two apart.

Two places used __mc68020__ to decide whether unaligned long accesses are allowed:

  • memcpy (platform/amiga/os3/string.S) skipped the AttnFlags test and copied misaligned buffers with move.l. That's the crash in the report.
  • iso_read_le32() (backends/iso9660/iso9660.h) loaded both-byte-order fields with one move.l, with no fallback at run time. Rock Ridge fields can sit at odd addresses: mkisofs puts PX right after a 5-byte RR entry, at offset 39 of every .. record. So a 68000 crashed on the first directory listing of any such disc. The patch in the issue doesn't cover this.

Both affect the 68000 disk build and the ROM build, since ROM inherits the 68000 CPU flags.

Commits

  1. amiga: keep the 68000 builds off unaligned long accesses. The 68020 build defines ODFS_M68K_UNALIGNED_OK, and both places test that. A build without it takes the safe path. The 68020 build stays byte-for-byte identical; the 68000 build grows by 592 bytes and the ROM by 48.
  2. amiga: tune the 68000 builds for the 68000. Drops -mtune=68020-60 from the 68000 disk and ROM builds; that tuning predates the 68020 build. One exception: handler_main.c (the packet loop and the streaming read path) stays tuned for the 68020–60 in the 68000 disk build. Tuned for the 68000, that path costs a 68040 one E-clock tick per Read(), −0.85% sequential read. GCC has no per-function tuning on m68k, so it's set per object. The 68020 build and the LTO-linked ROM leave it out. The 68000 build shrinks by 384 bytes and the ROM by 216.

Testing

Emulated A600: cycle-exact 68000, Kickstart 3.2.3, ATAPI CD on Gayle IDE.

Disc main (68000 build) this branch (68000 and ROM builds)
Plain ISO (CDBench fixture) Exception 3 (000022d8 …) in memcpy during CDBench listing and CDBench complete, no exceptions
Rock Ridge (mkisofs -R) Exception 3 (00002032 …) in iso_read_le32 on the first listing full listing, long names, symlink and file copy correct, no exceptions

CDBench, old tuning (all 68020–60) → this PR (all three runs per boot agree):

68000 rig, disk 68000 rig, ROM 68040 rig, disk 68040 rig, ROM
deep lock (µs) 8485 → 7714 35856 → 32921 148 → 139 619 → 575
open from lock (µs) 10549 → 10120 24210 → 23013 164 → 157 402 → 389
small open+read (µs) 43913 → 42800 123633 → 118545 318 → 298 1970 → 1934
Discovery (ent/s) 55 → 53 (cold, PIO-bound) 48 → 48 9432 → 9668 7200 → 7285
seq read (KiB/s) 90 → 89 (PIO-bound) 90 → 90 209126 → 208998 (same tick) 205340 → 205326

Tuning everything for the 68000, including handler_main.c, gave 207349 KiB/s sequential read on the 68040 (one tick lower). That was the same under four different free-memory layouts, which is why handler_main.c keeps the 68020–60 tuning.

The 68020 build is unchanged (byte-identical to main), so its CDBench figures can't move.

make check, golden-check and malformed-check pass; amiga, amiga-020, rom, amiga-test and rom-test build within their size limits.

Thanks to carlosbragatto for the report and the analysis.

The 68000 builds stop with an address error (80000003) on a 68000 as
soon as they list an ISO 9660 directory or mount an audio CD, as
carlosbragatto found and analysed in #61.

memcpy only asks exec whether the CPU allows unaligned long moves when
__mc68020__ is not defined, and iso_read_le32() loads both-byte-order
fields with one move.l when it is. GCC's __mc680x0__ macros follow
-mtune, though, not the -m option: the 68000 builds use
-m68000 -mtune=68020-60 and get __mc68020__ to __mc68060__ as well.
Their predefined macros are the same as those of
-m68020 -mtune=68020-60, so no compiler macro can tell the two apart.

So the 68000 and ROM builds used long moves for every misaligned copy
of more than 16 bytes, and iso_read_le32() read fields with move.l
where a Rock Ridge entry can put them at an odd address: SUSP entries
are not padded, so the PX entry after mkisofs' five-byte RR entry
starts at an odd offset in every ".." record.

Let the Makefile say it instead: the 68020 build defines
ODFS_M68K_UNALIGNED_OK, and both places test that. A build without it
takes the safe path, so a new variant that forgets it gets slower
code, not an address error.

The 68020 build stays byte for byte the same. The 68000 build grows by
592 bytes and the ROM build by 48: iso_read_le32() reads bytes now.
The 68000 disk and ROM builds were tuned for the 68020 to 68060
(-mtune=68020-60). That dates from when the 68000 build was the only
one. The 68020 build serves those CPUs now, and the tuning costs the
68000: GCC replaces moveq with clr.l and cmpi.l, swap with long
shifts, and shift-and-add with mulu, all of which take a 68000 more
cycles.

Keep the 68020-60 tuning for handler_main.c alone. It holds the
packet loop and the streaming read path, and tuned for the 68000 they
cost a 68040 one E-clock tick per Read(): GCC keeps the file handle in
a data register and moves it to an address register for each access,
and builds shifts from swap. That is -0.85% sequential read in
CDBench, the same under four different free-memory layouts. GCC has
no per-function tuning on m68k, so the Makefile sets it per object.
The 68020 build and the LTO-linked ROM profile leave it out; the ROM
reads as fast either way.

CDBench, 68000 disk build, all-68020-60 -> this:

  68040 rig        seq read   209126 -> 208998 KiB/s (same tick)
                   deep lock     148 -> 139 us
                   small o+r     318 -> 298 us
                   Discovery    9432 -> 9668 entries/s
  A600, 68000      deep lock    8485 -> 7714 us
                   open/lock   10549 -> 10120 us
                   small o+r   43913 -> 42800 us

and for the ROM build, all-68020-60 -> all-68000:

  68040 rig        deep lock     619 -> 575 us
                   small o+r    1970 -> 1934 us
  A600, 68000      deep lock   35856 -> 32921 us
                   small o+r  123633 -> 118545 us

The A600 runs use a cycle-exact 68000 with KS 3.2.3 and an ATAPI CD
on Gayle IDE; sequential read there is limited by the PIO transfers
and stays at 90 KiB/s. The 68000 build shrinks by 384 bytes and the
ROM build by 216. The 68020 build stays byte for byte the same.
@reinauer
reinauer merged commit d9adb14 into main Nov 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

68000 builds crash with 80000003: memcpy uses long moves for misaligned copies (__mc68020__ is defined by -mtune=68020-60)

1 participant