arch/x86/lib/insn-eval.c
Source file repositories/reference/linux-study-clean/arch/x86/lib/insn-eval.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/lib/insn-eval.c- Extension
.c- Size
- 49380 bytes
- Lines
- 1822
- Domain
- Architecture Layer
- Bucket
- arch/x86
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/kernel.hlinux/string.hlinux/ratelimit.hlinux/mmu_context.hasm/desc_defs.hasm/desc.hasm/inat.hasm/insn.hasm/insn-eval.hasm/ldt.hasm/msr.hasm/vm86.h
Detected Declarations
enum reg_typefunction is_string_insnfunction insn_has_rep_prefixfunction for_each_insn_prefixfunction get_seg_reg_override_idxfunction check_seg_overridesfunction resolve_default_segfunction usedfunction resolve_seg_regfunction get_segment_selectorfunction pt_regs_offsetfunction get_regnofunction get_reg_offsetfunction get_reg_offset_16function get_descfunction insn_get_seg_basefunction get_seg_limitfunction insn_get_code_seg_paramsfunction insn_get_modrm_rm_offfunction insn_get_modrm_reg_offfunction insn_get_modrm_reg_ptrfunction get_seg_base_limitfunction get_eff_addr_regfunction get_eff_addr_modrmfunction get_eff_addr_modrm_16function get_eff_addr_sibfunction get_addr_ref_16function get_addr_ref_32function get_addr_ref_64function insn_get_addr_reffunction insn_get_effective_ipfunction insn_fetch_from_userfunction insn_fetch_from_user_inatomicfunction insn_decode_from_regsfunction insn_decode_mmiofunction insn_is_nopfunction for_each_insn_prefixfunction for_each_insn_prefix
Annotated Snippet
switch (attr) {
case INAT_MAKE_PREFIX(INAT_PFX_CS):
idx = INAT_SEG_REG_CS;
num_overrides++;
break;
case INAT_MAKE_PREFIX(INAT_PFX_SS):
idx = INAT_SEG_REG_SS;
num_overrides++;
break;
case INAT_MAKE_PREFIX(INAT_PFX_DS):
idx = INAT_SEG_REG_DS;
num_overrides++;
break;
case INAT_MAKE_PREFIX(INAT_PFX_ES):
idx = INAT_SEG_REG_ES;
num_overrides++;
break;
case INAT_MAKE_PREFIX(INAT_PFX_FS):
idx = INAT_SEG_REG_FS;
num_overrides++;
break;
case INAT_MAKE_PREFIX(INAT_PFX_GS):
idx = INAT_SEG_REG_GS;
num_overrides++;
break;
/* No default action needed. */
}
}
/* More than one segment override prefix leads to undefined behavior. */
if (num_overrides > 1)
return -EINVAL;
return idx;
}
/**
* check_seg_overrides() - check if segment override prefixes are allowed
* @insn: Valid instruction with segment override prefixes
* @regoff: Operand offset, in pt_regs, for which the check is performed
*
* For a particular register used in register-indirect addressing, determine if
* segment override prefixes can be used. Specifically, no overrides are allowed
* for rDI if used with a string instruction.
*
* Returns:
*
* True if segment override prefixes can be used with the register indicated
* in @regoff. False if otherwise.
*/
static bool check_seg_overrides(struct insn *insn, int regoff)
{
if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
return false;
return true;
}
/**
* resolve_default_seg() - resolve default segment register index for an operand
* @insn: Instruction with opcode and address size. Must be valid.
* @regs: Register values as seen when entering kernel mode
* @off: Operand offset, in pt_regs, for which resolution is needed
*
* Resolve the default segment register index associated with the instruction
* operand register indicated by @off. Such index is resolved based on defaults
* described in the Intel Software Development Manual.
*
* Returns:
*
* If in protected mode, a constant identifying the segment register to use,
* among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
*
* -EINVAL in case of error.
*/
static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
{
if (any_64bit_mode(regs))
return INAT_SEG_REG_IGNORE;
/*
* Resolve the default segment register as described in Section 3.7.4
* of the Intel Software Development Manual Vol. 1:
*
* + DS for all references involving r[ABCD]X, and rSI.
* + If used in a string instruction, ES for rDI. Otherwise, DS.
* + AX, CX and DX are not valid register operands in 16-bit address
* encodings but are valid for 32-bit and 64-bit encodings.
* + -EDOM is reserved to identify for cases in which no register
* is used (i.e., displacement-only addressing). Use DS.
* + SS for rSP or rBP.
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/string.h`, `linux/ratelimit.h`, `linux/mmu_context.h`, `asm/desc_defs.h`, `asm/desc.h`, `asm/inat.h`, `asm/insn.h`.
- Detected declarations: `enum reg_type`, `function is_string_insn`, `function insn_has_rep_prefix`, `function for_each_insn_prefix`, `function get_seg_reg_override_idx`, `function check_seg_overrides`, `function resolve_default_seg`, `function used`, `function resolve_seg_reg`, `function get_segment_selector`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.