arch/mips/tools/loongson3-llsc-check.c
Source file repositories/reference/linux-study-clean/arch/mips/tools/loongson3-llsc-check.c
File Facts
- System
- Linux kernel
- Corpus path
arch/mips/tools/loongson3-llsc-check.c- Extension
.c- Size
- 6206 bytes
- Lines
- 310
- Domain
- Architecture Layer
- Bucket
- arch/mips
- Inferred role
- Architecture Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
byteswap.helf.hendian.herrno.hfcntl.hinttypes.hstdbool.hstdio.hstdlib.hstring.hsys/mman.hsys/types.hsys/stat.hunistd.h
Detected Declarations
function usagefunction se16function is_llfunction is_scfunction is_syncfunction is_branchfunction check_llfunction check_codefunction main
Annotated Snippet
switch ((insn >> 16) & 0x1f) {
case REGIMM_BGEZ:
case REGIMM_BGEZL:
case REGIMM_BGEZAL:
case REGIMM_BGEZALL:
case REGIMM_BLTZ:
case REGIMM_BLTZL:
case REGIMM_BLTZAL:
case REGIMM_BLTZALL:
*off = se16(insn) + 1;
return true;
default:
return false;
}
default:
return false;
}
}
static int check_ll(uint64_t pc, uint32_t *code, size_t sz)
{
ssize_t i, max, sc_pos;
int off;
/*
* Every LL must be preceded by a sync instruction in order to ensure
* that instruction reordering doesn't allow a prior memory access to
* execute after the LL & cause erroneous results.
*/
if (!is_sync(le32toh(code[-1]))) {
fprintf(stderr, "%" PRIx64 ": LL not preceded by sync\n", pc);
return -EINVAL;
}
/* Find the matching SC instruction */
max = sz / 4;
for (sc_pos = 0; sc_pos < max; sc_pos++) {
if (is_sc(le32toh(code[sc_pos])))
break;
}
if (sc_pos >= max) {
fprintf(stderr, "%" PRIx64 ": LL has no matching SC\n", pc);
return -EINVAL;
}
/*
* Check branches within the LL/SC loop target sync instructions,
* ensuring that speculative execution can't generate memory accesses
* due to instructions outside of the loop.
*/
for (i = 0; i < sc_pos; i++) {
if (!is_branch(le32toh(code[i]), &off))
continue;
/*
* If the branch target is within the LL/SC loop then we don't
* need to worry about it.
*/
if ((off >= -i) && (off <= sc_pos))
continue;
/* If the branch targets a sync instruction we're all good... */
if (is_sync(le32toh(code[i + off])))
continue;
/* ...but if not, we have a problem */
fprintf(stderr, "%" PRIx64 ": Branch target not a sync\n",
pc + (i * 4));
return -EINVAL;
}
return 0;
}
static int check_code(uint64_t pc, uint32_t *code, size_t sz)
{
int err = 0;
if (sz % 4) {
fprintf(stderr, "%" PRIx64 ": Section size not a multiple of 4\n",
pc);
err = -EINVAL;
sz -= (sz % 4);
}
if (is_ll(le32toh(code[0]))) {
fprintf(stderr, "%" PRIx64 ": First instruction in section is an LL\n",
pc);
Annotation
- Immediate include surface: `byteswap.h`, `elf.h`, `endian.h`, `errno.h`, `fcntl.h`, `inttypes.h`, `stdbool.h`, `stdio.h`.
- Detected declarations: `function usage`, `function se16`, `function is_ll`, `function is_sc`, `function is_sync`, `function is_branch`, `function check_ll`, `function check_code`, `function main`.
- Atlas domain: Architecture Layer / arch/mips.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.