arch/sh/kernel/ftrace.c
Source file repositories/reference/linux-study-clean/arch/sh/kernel/ftrace.c
File Facts
- System
- Linux kernel
- Corpus path
arch/sh/kernel/ftrace.c- Extension
.c- Size
- 10247 bytes
- Lines
- 366
- Domain
- Architecture Layer
- Bucket
- arch/sh
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/uaccess.hlinux/ftrace.hlinux/string.hlinux/init.hlinux/io.hlinux/kernel.hasm/ftrace.hasm/cacheflush.hasm/unistd.htrace/syscall.h
Detected Declarations
function afunction clear_mod_flagfunction ftrace_mod_codefunction arch_ftrace_nmi_enterfunction arch_ftrace_nmi_exitfunction wait_for_nmi_and_set_mod_flagfunction wait_for_nmifunction do_ftrace_mod_codefunction ftrace_modify_codefunction ftrace_update_ftrace_funcfunction ftrace_make_nopfunction ftrace_make_callfunction ftrace_modfunction ftrace_enable_ftrace_graph_callerfunction ftrace_disable_ftrace_graph_callerfunction prepare_ftrace_return
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2008 Matt Fleming <matt@console-pimps.org>
* Copyright (C) 2008 Paul Mundt <lethal@linux-sh.org>
*
* Code for replacing ftrace calls with jumps.
*
* Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
*
* Thanks goes to Ingo Molnar, for suggesting the idea.
* Mathieu Desnoyers, for suggesting postponing the modifications.
* Arjan van de Ven, for keeping me straight, and explaining to me
* the dangers of modifying code on the run.
*/
#include <linux/uaccess.h>
#include <linux/ftrace.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <asm/ftrace.h>
#include <asm/cacheflush.h>
#include <asm/unistd.h>
#include <trace/syscall.h>
#ifdef CONFIG_DYNAMIC_FTRACE
static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
static unsigned char ftrace_nop[4];
/*
* If we're trying to nop out a call to a function, we instead
* place a call to the address after the memory table.
*
* 8c011060 <a>:
* 8c011060: 02 d1 mov.l 8c01106c <a+0xc>,r1
* 8c011062: 22 4f sts.l pr,@-r15
* 8c011064: 02 c7 mova 8c011070 <a+0x10>,r0
* 8c011066: 2b 41 jmp @r1
* 8c011068: 2a 40 lds r0,pr
* 8c01106a: 09 00 nop
* 8c01106c: 68 24 .word 0x2468 <--- ip
* 8c01106e: 1d 8c .word 0x8c1d
* 8c011070: 26 4f lds.l @r15+,pr <--- ip + MCOUNT_INSN_SIZE
*
* We write 0x8c011070 to 0x8c01106c so that on entry to a() we branch
* past the _mcount call and continue executing code like normal.
*/
static unsigned char *ftrace_nop_replace(unsigned long ip)
{
__raw_writel(ip + MCOUNT_INSN_SIZE, ftrace_nop);
return ftrace_nop;
}
static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
{
/* Place the address in the memory table. */
__raw_writel(addr, ftrace_replaced_code);
/*
* No locking needed, this must be called via kstop_machine
* which in essence is like running on a uniprocessor machine.
*/
return ftrace_replaced_code;
}
/*
* Modifying code must take extra care. On an SMP machine, if
* the code being modified is also being executed on another CPU
* that CPU will have undefined results and possibly take a GPF.
* We use kstop_machine to stop other CPUS from executing code.
* But this does not stop NMIs from happening. We still need
* to protect against that. We separate out the modification of
* the code to take care of this.
*
* Two buffers are added: An IP buffer and a "code" buffer.
*
* 1) Put the instruction pointer into the IP buffer
* and the new code into the "code" buffer.
* 2) Wait for any running NMIs to finish and set a flag that says
* we are modifying code, it is done in an atomic operation.
* 3) Write the code
* 4) clear the flag.
* 5) Wait for any running NMIs to finish.
*
* If an NMI is executed, the first thing it does is to call
* "ftrace_nmi_enter". This will check if the flag is set to write
* and if it is, it will write what is in the IP and "code" buffers.
*
* The trick is, it does not matter if everyone is writing the same
* content to the code location. Also, if a CPU is executing code
Annotation
- Immediate include surface: `linux/uaccess.h`, `linux/ftrace.h`, `linux/string.h`, `linux/init.h`, `linux/io.h`, `linux/kernel.h`, `asm/ftrace.h`, `asm/cacheflush.h`.
- Detected declarations: `function a`, `function clear_mod_flag`, `function ftrace_mod_code`, `function arch_ftrace_nmi_enter`, `function arch_ftrace_nmi_exit`, `function wait_for_nmi_and_set_mod_flag`, `function wait_for_nmi`, `function do_ftrace_mod_code`, `function ftrace_modify_code`, `function ftrace_update_ftrace_func`.
- Atlas domain: Architecture Layer / arch/sh.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.