Lines Matching +full:smp +full:- +full:offset

1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "SMP alternatives: " fmt
21 #include <asm/text-patching.h>
32 #include <asm/asm-prototypes.h>
39 #define MAX_PATCH_LEN (255-1)
60 __setup("debug-alternative", debug_alt);
69 __setup("noreplace-smp", setup_noreplace_smp);
86 for (j = 0; j < (len) - 1; j++) \
135 * eventually text-poked into the kernel image.
145 * for every single-byte NOP, try to generate the maximally available NOP of
147 * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and
187 if (insn->opcode.bytes[0] == 0x90 && in insn_is_nop()
188 (!insn->prefixes.nbytes || insn->prefixes.bytes[0] != 0xF3)) in insn_is_nop()
192 if (insn->opcode.bytes[0] == 0x0F && insn->opcode.bytes[1] == 0x1F) in insn_is_nop()
201 * Find the offset of the first non-NOP instruction starting at @offset
204 static int skip_nops(u8 *buf, int offset, int len) in skip_nops() argument
208 for (; offset < len; offset += insn.length) { in skip_nops()
209 if (insn_decode_kernel(&insn, &buf[offset])) in skip_nops()
216 return offset; in skip_nops()
242 add_nop(buf + nop, next - nop); in optimize_nops()
255 * The source offset is:
257 * src_imm = target - src_next_ip (1)
259 * and the target offset is:
261 * dst_imm = target - dst_next_ip (2)
269 * dst_imm = (src_imm + src_next_ip) - dst_next_ip (3)
280 * dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset)
281 * = src_imm + src - dst + ip_offset - ip_offset
282 * = src_imm + src - dst (5)
291 BUG_ON((v >> 31) != (v >> (n_-1))); \
308 bool need_reloc(unsigned long offset, u8 *src, size_t src_len) in need_reloc() argument
310 u8 *target = src + offset; in need_reloc()
342 repl - instr); in __apply_relocation()
350 imm += repl - instr; in __apply_relocation()
351 imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE; in __apply_relocation()
356 memset(&buf[i+2], INT3_INSN_OPCODE, insn.length - 2); in __apply_relocation()
366 repl - instr); in __apply_relocation()
378 /* Low-level backend functions usable from alternative code replacements. */
402 if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) { in alt_replace_call()
403 pr_err("ALT_FLAG_DIRECT_CALL set for a non-call replacement instruction\n"); in alt_replace_call()
407 if (a->instrlen != 6 || in alt_replace_call()
419 target = *(void **)(instr + a->instrlen + disp); in alt_replace_call()
428 /* (BUG_func - .) + (target - BUG_func) := target - . */ in alt_replace_call()
429 *(s32 *)(insn_buff + 1) += target - bug; in alt_replace_call()
439 return (u8 *)&i->instr_offset + i->instr_offset; in instr_va()
444 * before SMP is initialized to avoid SMP problems with self modifying code.
460 DPRINTK(ALT, "alt table %px, -> %px", start, end); in apply_alternatives()
466 * conversion and triggers a false-positive out-of-bound report. in apply_alternatives()
492 u8 len = max(a->instrlen, b->instrlen); in apply_alternatives()
493 a->instrlen = b->instrlen = len; in apply_alternatives()
499 replacement = (u8 *)&a->repl_offset + a->repl_offset; in apply_alternatives()
502 BUG_ON(a->instrlen > sizeof(insn_buff)); in apply_alternatives()
503 BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); in apply_alternatives()
507 * - feature is present in apply_alternatives()
508 * - feature not present but ALT_FLAG_NOT is set to mean, in apply_alternatives()
511 if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { in apply_alternatives()
512 memcpy(insn_buff, wr_instr, a->instrlen); in apply_alternatives()
513 optimize_nops(instr, insn_buff, a->instrlen); in apply_alternatives()
514 text_poke_early(wr_instr, insn_buff, a->instrlen); in apply_alternatives()
519 a->cpuid >> 5, in apply_alternatives()
520 a->cpuid & 0x1f, in apply_alternatives()
521 instr, instr, a->instrlen, in apply_alternatives()
522 replacement, a->replacementlen, a->flags); in apply_alternatives()
524 memcpy(insn_buff, wr_replacement, a->replacementlen); in apply_alternatives()
525 insn_buff_sz = a->replacementlen; in apply_alternatives()
527 if (a->flags & ALT_FLAG_DIRECT_CALL) { in apply_alternatives()
534 for (; insn_buff_sz < a->instrlen; insn_buff_sz++) in apply_alternatives()
537 apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen); in apply_alternatives()
539 DUMP_BYTES(ALT, wr_instr, a->instrlen, "%px: old_insn: ", instr); in apply_alternatives()
540 DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); in apply_alternatives()
551 /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ in is_jcc32()
552 return insn->opcode.bytes[0] == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80; in is_jcc32()
576 return -1; in emit_indirect()
581 reg -= 8; in emit_indirect()
595 u8 op = insn->opcode.bytes[0]; in emit_call_track_retpoline()
600 * tail-calls. Deal with them. in emit_call_track_retpoline()
604 op = insn->opcode.bytes[1]; in emit_call_track_retpoline()
608 if (insn->length == 6) in emit_call_track_retpoline()
609 bytes[i++] = 0x2e; /* CS-prefix */ in emit_call_track_retpoline()
629 return -1; in emit_call_track_retpoline()
632 WARN_ON_ONCE(i != insn->length); in emit_call_track_retpoline()
659 target = addr + insn->length + insn->immediate.value; in patch_retpoline()
660 reg = target - __x86_indirect_thunk_array; in patch_retpoline()
663 return -1; in patch_retpoline()
673 return -1; in patch_retpoline()
676 op = insn->opcode.bytes[0]; in patch_retpoline()
692 cc = insn->opcode.bytes[1] & 0xf; in patch_retpoline()
696 bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */ in patch_retpoline()
722 if (op == JMP32_INSN_OPCODE && i < insn->length) in patch_retpoline()
725 for (; i < insn->length;) in patch_retpoline()
732 * Generated by 'objtool --retpoline'.
785 * Rewrite the compiler generated return thunk tail-calls.
808 for (; i < insn->length;) in patch_return()
839 "missing return thunk: %pS-%pS: %*ph", in apply_returns()
896 * Generated by: objtool --ibt
911 poison_cfi(addr - 16, wr_addr - 16); in apply_seal_endbr()
977 func -= cfi_get_offset(); in cfi_get_func_hash()
1002 * Re-hash the CFI hash with a boot-time seed while making sure the result is
1008 while (unlikely(is_endbr(hash) || is_endbr(-hash))) { in cfi_rehash()
1020 return -EINVAL; in cfi_parse_cmdline()
1070 * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6
1071 * addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4
1092 #define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
1107 #define fineibt_caller_size (fineibt_caller_end - fineibt_caller_start)
1110 #define fineibt_caller_jmp (fineibt_caller_size - 2)
1129 return -*(u32 *)(addr + 2); in decode_caller_hash()
1133 return -*(u32 *)(addr + 2); in decode_caller_hash()
1154 addr -= fineibt_caller_size; in cfi_disable_callers()
1170 * Re-enable kCFI, undo what cfi_disable_callers() did. in cfi_enable_callers()
1180 addr -= fineibt_caller_size; in cfi_enable_callers()
1205 return -EINVAL; in cfi_rand_preamble()
1226 return -EINVAL; in cfi_rewrite_preamble()
1258 addr -= fineibt_caller_size; in cfi_rand_callers()
1262 hash = -cfi_rehash(hash); in cfi_rand_callers()
1279 addr -= fineibt_caller_size; in cfi_rewrite_callers()
1350 /* place the FineIBT preamble at func()-16 */ in __apply_fineibt()
1355 /* rewrite the callers to target func()-16 */ in __apply_fineibt()
1486 struct smp_alt_module *smp; in alternatives_smp_module_add() local
1496 smp = kzalloc(sizeof(*smp), GFP_KERNEL); in alternatives_smp_module_add()
1497 if (NULL == smp) in alternatives_smp_module_add()
1498 /* we'll run the (safe but slow) SMP code then ... */ in alternatives_smp_module_add()
1501 smp->mod = mod; in alternatives_smp_module_add()
1502 smp->name = name; in alternatives_smp_module_add()
1503 smp->locks = locks; in alternatives_smp_module_add()
1504 smp->locks_end = locks_end; in alternatives_smp_module_add()
1505 smp->text = text; in alternatives_smp_module_add()
1506 smp->text_end = text_end; in alternatives_smp_module_add()
1507 DPRINTK(SMP, "locks %p -> %p, text %p -> %p, name %s\n", in alternatives_smp_module_add()
1508 smp->locks, smp->locks_end, in alternatives_smp_module_add()
1509 smp->text, smp->text_end, smp->name); in alternatives_smp_module_add()
1511 list_add_tail(&smp->next, &smp_alt_modules); in alternatives_smp_module_add()
1524 if (mod != item->mod) in alternatives_smp_module_del()
1526 list_del(&item->next); in alternatives_smp_module_del()
1543 pr_info("switching to SMP code\n"); in alternatives_enable_smp()
1548 alternatives_smp_lock(mod->locks, mod->locks_end, in alternatives_enable_smp()
1549 mod->text, mod->text_end); in alternatives_enable_smp()
1556 * Return 1 if the address range is reserved for SMP-alternatives.
1569 if (mod->text > text_end || mod->text_end < text_start) in alternatives_text_reserved()
1571 for (poff = mod->locks; poff < mod->locks_end; poff++) { in alternatives_text_reserved()
1584 * Self-test for the INT3 based CALL emulation code.
1608 " .size int3_magic, .-int3_magic\n"
1619 struct pt_regs *regs = args->regs; in int3_exception_notify()
1629 if (regs->ip - INT3_INSN_SIZE != selftest) in int3_exception_notify()
1641 .priority = INT_MAX-1, /* last */ in int3_selftest()
1648 * Basically: int3_magic(&val); but really complicated :-) in int3_selftest()
1679 * instruction and additionally on x86_64 we get a RIP-relative LEA: in alt_reloc_selftest()
1682 * call +0 # 5d5: R_X86_64_PLT32 __alt_reloc_selftest-0x4 in alt_reloc_selftest()
1756 free_init_pages("SMP alternatives", in alternative_instructions()
1769 * text_poke_early - Update instructions on a live kernel at boot time
1788 * Modules text is marked initially as non-executable, so the in text_poke_early()
1789 * code cannot be running and speculative code-fetches are in text_poke_early()
1814 * temporary page-table mappings that are required for these write operations to
1925 * flush_tlb_mm_range(), which is intended for non-global PTEs. in __text_poke()
1930 * The lock is not really needed, but this allows to avoid open-coding. in __text_poke()
1970 * Loading the previous page-table hierarchy requires a serializing in __text_poke()
1972 * Xen-PV is assumed to serialize execution in a similar manner. in __text_poke()
1998 * text_poke - Update instructions on a live kernel
2021 * text_poke_kgdb - Update instructions on a live kernel by kgdb
2052 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); in text_poke_copy_locked()
2061 * text_poke_copy - Copy instructions into (an unused part of) RX memory
2081 * text_poke_set - memset into (an unused part of) RX memory
2102 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); in text_poke_set()
2150 if (!raw_atomic_inc_not_zero(&desc->refs)) in try_get_desc()
2161 raw_atomic_dec(&desc->refs); in put_desc()
2166 return _stext + tp->rel_addr; in text_poke_addr()
2174 return -1; in patch_cmp()
2192 * bp_desc with non-zero refcount: in poke_int3_handler()
2207 ip = (void *) regs->ip - INT3_INSN_SIZE; in poke_int3_handler()
2212 if (unlikely(desc->nr_entries > 1)) { in poke_int3_handler()
2213 tp = __inline_bsearch(ip, desc->vec, desc->nr_entries, in poke_int3_handler()
2219 tp = desc->vec; in poke_int3_handler()
2224 ip += tp->len; in poke_int3_handler()
2226 switch (tp->opcode) { in poke_int3_handler()
2239 int3_emulate_call(regs, (long)ip + tp->disp); in poke_int3_handler()
2244 int3_emulate_jmp(regs, (long)ip + tp->disp); in poke_int3_handler()
2248 int3_emulate_jcc(regs, tp->opcode & 0xf, (long)ip, tp->disp); in poke_int3_handler()
2267 * text_poke_bp_batch() -- update instructions on live kernel on SMP
2271 * Modify multi-byte instruction by using int3 breakpoint on SMP.
2276 * - For each entry in the vector:
2277 * - add a int3 trap to the address that will be patched
2278 * - sync cores
2279 * - For each entry in the vector:
2280 * - update all but the first byte of the patched range
2281 * - sync cores
2282 * - For each entry in the vector:
2283 * - replace the first byte (int3) by the first byte of
2285 * - sync cores
2300 * ensure reading a non-zero refcount provides up to date bp_desc data. in text_poke_bp_batch()
2339 if (len - INT3_INSN_SIZE > 0) { in text_poke_bp_batch()
2342 len - INT3_INSN_SIZE); in text_poke_bp_batch()
2352 len - INT3_INSN_SIZE); in text_poke_bp_batch()
2361 * - write INT3 byte in text_poke_bp_batch()
2362 * - IPI-SYNC in text_poke_bp_batch()
2363 * - write instruction tail in text_poke_bp_batch()
2368 * - emit RECORD_TEXT_POKE with the new instruction in text_poke_bp_batch()
2369 * - IPI-SYNC in text_poke_bp_batch()
2370 * - write first byte in text_poke_bp_batch()
2371 * - IPI-SYNC in text_poke_bp_batch()
2428 memcpy((void *)tp->text, opcode+i, len-i); in text_poke_loc_init()
2435 tp->rel_addr = addr - (void *)_stext; in text_poke_loc_init()
2436 tp->len = len; in text_poke_loc_init()
2437 tp->opcode = insn.opcode.bytes[0]; in text_poke_loc_init()
2443 tp->opcode = insn.opcode.bytes[1] - 0x10; in text_poke_loc_init()
2446 switch (tp->opcode) { in text_poke_loc_init()
2455 BUG_ON(tp->text[i] != INT3_INSN_OPCODE); in text_poke_loc_init()
2462 switch (tp->opcode) { in text_poke_loc_init()
2471 tp->disp = insn.immediate.value; in text_poke_loc_init()
2476 case 2: /* NOP2 -- emulate as JMP8+0 */ in text_poke_loc_init()
2478 tp->opcode = JMP8_INSN_OPCODE; in text_poke_loc_init()
2479 tp->disp = 0; in text_poke_loc_init()
2482 case 5: /* NOP5 -- emulate as JMP32+0 */ in text_poke_loc_init()
2484 tp->opcode = JMP32_INSN_OPCODE; in text_poke_loc_init()
2485 tp->disp = 0; in text_poke_loc_init()
2509 tp = &tp_vec[tp_vec_nr - 1]; in tp_order_fail()
2540 * text_poke_bp() -- update instructions on live kernel on SMP