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++) \
131 * for every single-byte NOP, try to generate the maximally available NOP of
133 * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and
173 if (insn->opcode.bytes[0] == 0x90 && in insn_is_nop()
174 (!insn->prefixes.nbytes || insn->prefixes.bytes[0] != 0xF3)) in insn_is_nop()
178 if (insn->opcode.bytes[0] == 0x0F && insn->opcode.bytes[1] == 0x1F) in insn_is_nop()
187 * Find the offset of the first non-NOP instruction starting at @offset
190 static int skip_nops(u8 *instr, int offset, int len) in skip_nops() argument
194 for (; offset < len; offset += insn.length) { in skip_nops()
195 if (insn_decode_kernel(&insn, &instr[offset])) in skip_nops()
202 return offset; in skip_nops()
212 int i = *next - insn->length; in __optimize_nops()
214 switch (insn->opcode.bytes[0]) { in __optimize_nops()
218 *target = *next + insn->immediate.value; in __optimize_nops()
229 add_nop(instr + nop, *next - nop); in __optimize_nops()
275 * The source offset is:
277 * src_imm = target - src_next_ip (1)
279 * and the target offset is:
281 * dst_imm = target - dst_next_ip (2)
289 * dst_imm = (src_imm + src_next_ip) - dst_next_ip (3)
300 * dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset)
301 * = src_imm + src - dst + ip_offset - ip_offset
302 * = src_imm + src - dst (5)
311 BUG_ON((v >> 31) != (v >> (n_-1))); \
328 bool need_reloc(unsigned long offset, u8 *src, size_t src_len) in need_reloc() argument
330 u8 *target = src + offset; in need_reloc()
368 src - dest); in apply_relocation()
376 imm += src - dest; in apply_relocation()
377 imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE; in apply_relocation()
382 memset(&buf[i+2], INT3_INSN_OPCODE, insn.length - 2); in apply_relocation()
392 src - dest); in apply_relocation()
398 /* Low-level backend functions usable from alternative code replacements. */
420 if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) { in alt_replace_call()
421 pr_err("ALT_FLAG_DIRECT_CALL set for a non-call replacement instruction\n"); in alt_replace_call()
425 if (a->instrlen != 6 || in alt_replace_call()
437 target = *(void **)(instr + a->instrlen + disp); in alt_replace_call()
446 /* (BUG_func - .) + (target - BUG_func) := target - . */ in alt_replace_call()
447 *(s32 *)(insn_buff + 1) += target - bug; in alt_replace_call()
457 * before SMP is initialized to avoid SMP problems with self modifying code.
472 DPRINTK(ALT, "alt table %px, -> %px", start, end); in apply_alternatives()
478 * conversion and triggers a false-positive out-of-bound report. in apply_alternatives()
496 instr = (u8 *)&a->instr_offset + a->instr_offset; in apply_alternatives()
497 replacement = (u8 *)&a->repl_offset + a->repl_offset; in apply_alternatives()
498 BUG_ON(a->instrlen > sizeof(insn_buff)); in apply_alternatives()
499 BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); in apply_alternatives()
503 * - feature is present in apply_alternatives()
504 * - feature not present but ALT_FLAG_NOT is set to mean, in apply_alternatives()
507 if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { in apply_alternatives()
508 optimize_nops_inplace(instr, a->instrlen); in apply_alternatives()
513 a->cpuid >> 5, in apply_alternatives()
514 a->cpuid & 0x1f, in apply_alternatives()
515 instr, instr, a->instrlen, in apply_alternatives()
516 replacement, a->replacementlen, a->flags); in apply_alternatives()
518 memcpy(insn_buff, replacement, a->replacementlen); in apply_alternatives()
519 insn_buff_sz = a->replacementlen; in apply_alternatives()
521 if (a->flags & ALT_FLAG_DIRECT_CALL) { in apply_alternatives()
527 for (; insn_buff_sz < a->instrlen; insn_buff_sz++) in apply_alternatives()
530 apply_relocation(insn_buff, a->instrlen, instr, replacement, a->replacementlen); in apply_alternatives()
532 DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); in apply_alternatives()
533 DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); in apply_alternatives()
544 /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ in is_jcc32()
545 return insn->opcode.bytes[0] == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80; in is_jcc32()
569 return -1; in emit_indirect()
574 reg -= 8; in emit_indirect()
588 u8 op = insn->opcode.bytes[0]; in emit_call_track_retpoline()
593 * tail-calls. Deal with them. in emit_call_track_retpoline()
597 op = insn->opcode.bytes[1]; in emit_call_track_retpoline()
601 if (insn->length == 6) in emit_call_track_retpoline()
602 bytes[i++] = 0x2e; /* CS-prefix */ in emit_call_track_retpoline()
622 return -1; in emit_call_track_retpoline()
625 WARN_ON_ONCE(i != insn->length); in emit_call_track_retpoline()
652 target = addr + insn->length + insn->immediate.value; in patch_retpoline()
653 reg = target - __x86_indirect_thunk_array; in patch_retpoline()
656 return -1; in patch_retpoline()
666 return -1; in patch_retpoline()
669 op = insn->opcode.bytes[0]; in patch_retpoline()
685 cc = insn->opcode.bytes[1] & 0xf; in patch_retpoline()
689 bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */ in patch_retpoline()
715 if (op == JMP32_INSN_OPCODE && i < insn->length) in patch_retpoline()
718 for (; i < insn->length;) in patch_retpoline()
725 * Generated by 'objtool --retpoline'.
776 * Rewrite the compiler generated return thunk tail-calls.
799 for (; i < insn->length;) in patch_return()
828 "missing return thunk: %pS-%pS: %*ph", in apply_returns()
882 * Generated by: objtool --ibt
896 poison_cfi(addr - 16); in apply_seal_endbr()
962 func -= cfi_get_offset(); in cfi_get_func_hash()
987 * Re-hash the CFI hash with a boot-time seed while making sure the result is
993 while (unlikely(is_endbr(hash) || is_endbr(-hash))) { in cfi_rehash()
1005 return -EINVAL; in cfi_parse_cmdline()
1055 * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6
1056 * addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4
1077 #define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
1092 #define fineibt_caller_size (fineibt_caller_end - fineibt_caller_start)
1095 #define fineibt_caller_jmp (fineibt_caller_size - 2)
1114 return -*(u32 *)(addr + 2); in decode_caller_hash()
1118 return -*(u32 *)(addr + 2); in decode_caller_hash()
1138 addr -= fineibt_caller_size; in cfi_disable_callers()
1152 * Re-enable kCFI, undo what cfi_disable_callers() did. in cfi_enable_callers()
1161 addr -= fineibt_caller_size; in cfi_enable_callers()
1184 return -EINVAL; in cfi_rand_preamble()
1204 return -EINVAL; in cfi_rewrite_preamble()
1234 addr -= fineibt_caller_size; in cfi_rand_callers()
1237 hash = -cfi_rehash(hash); in cfi_rand_callers()
1253 addr -= fineibt_caller_size; in cfi_rewrite_callers()
1322 /* place the FineIBT preamble at func()-16 */ in __apply_fineibt()
1327 /* rewrite the callers to target func()-16 */ in __apply_fineibt()
1459 struct smp_alt_module *smp; in alternatives_smp_module_add() local
1469 smp = kzalloc(sizeof(*smp), GFP_KERNEL); in alternatives_smp_module_add()
1470 if (NULL == smp) in alternatives_smp_module_add()
1471 /* we'll run the (safe but slow) SMP code then ... */ in alternatives_smp_module_add()
1474 smp->mod = mod; in alternatives_smp_module_add()
1475 smp->name = name; in alternatives_smp_module_add()
1476 smp->locks = locks; in alternatives_smp_module_add()
1477 smp->locks_end = locks_end; in alternatives_smp_module_add()
1478 smp->text = text; in alternatives_smp_module_add()
1479 smp->text_end = text_end; in alternatives_smp_module_add()
1480 DPRINTK(SMP, "locks %p -> %p, text %p -> %p, name %s\n", in alternatives_smp_module_add()
1481 smp->locks, smp->locks_end, in alternatives_smp_module_add()
1482 smp->text, smp->text_end, smp->name); in alternatives_smp_module_add()
1484 list_add_tail(&smp->next, &smp_alt_modules); in alternatives_smp_module_add()
1497 if (mod != item->mod) in alternatives_smp_module_del()
1499 list_del(&item->next); in alternatives_smp_module_del()
1516 pr_info("switching to SMP code\n"); in alternatives_enable_smp()
1521 alternatives_smp_lock(mod->locks, mod->locks_end, in alternatives_enable_smp()
1522 mod->text, mod->text_end); in alternatives_enable_smp()
1529 * Return 1 if the address range is reserved for SMP-alternatives.
1542 if (mod->text > text_end || mod->text_end < text_start) in alternatives_text_reserved()
1544 for (poff = mod->locks; poff < mod->locks_end; poff++) { in alternatives_text_reserved()
1557 * Self-test for the INT3 based CALL emulation code.
1581 " .size int3_magic, .-int3_magic\n"
1592 struct pt_regs *regs = args->regs; in int3_exception_notify()
1602 if (regs->ip - INT3_INSN_SIZE != selftest) in int3_exception_notify()
1614 .priority = INT_MAX-1, /* last */ in int3_selftest()
1621 * Basically: int3_magic(&val); but really complicated :-) in int3_selftest()
1652 * instruction and additionally on x86_64 we get a RIP-relative LEA: in alt_reloc_selftest()
1655 * call +0 # 5d5: R_X86_64_PLT32 __alt_reloc_selftest-0x4 in alt_reloc_selftest()
1729 free_init_pages("SMP alternatives", in alternative_instructions()
1742 * text_poke_early - Update instructions on a live kernel at boot time
1761 * Modules text is marked initially as non-executable, so the in text_poke_early()
1762 * code cannot be running and speculative code-fetches are in text_poke_early()
1787 * temporary page-table mappings that are required for these write operations to
1894 * flush_tlb_mm_range(), which is intended for non-global PTEs. in __text_poke()
1899 * The lock is not really needed, but this allows to avoid open-coding. in __text_poke()
1939 * Loading the previous page-table hierarchy requires a serializing in __text_poke()
1941 * Xen-PV is assumed to serialize execution in a similar manner. in __text_poke()
1967 * text_poke - Update instructions on a live kernel
1990 * text_poke_kgdb - Update instructions on a live kernel by kgdb
2021 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); in text_poke_copy_locked()
2030 * text_poke_copy - Copy instructions into (an unused part of) RX memory
2050 * text_poke_set - memset into (an unused part of) RX memory
2071 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); in text_poke_set()
2119 if (!raw_atomic_inc_not_zero(&desc->refs)) in try_get_desc()
2130 raw_atomic_dec(&desc->refs); in put_desc()
2135 return _stext + tp->rel_addr; in text_poke_addr()
2143 return -1; in patch_cmp()
2161 * bp_desc with non-zero refcount: in poke_int3_handler()
2176 ip = (void *) regs->ip - INT3_INSN_SIZE; in poke_int3_handler()
2181 if (unlikely(desc->nr_entries > 1)) { in poke_int3_handler()
2182 tp = __inline_bsearch(ip, desc->vec, desc->nr_entries, in poke_int3_handler()
2188 tp = desc->vec; in poke_int3_handler()
2193 ip += tp->len; in poke_int3_handler()
2195 switch (tp->opcode) { in poke_int3_handler()
2208 int3_emulate_call(regs, (long)ip + tp->disp); in poke_int3_handler()
2213 int3_emulate_jmp(regs, (long)ip + tp->disp); in poke_int3_handler()
2217 int3_emulate_jcc(regs, tp->opcode & 0xf, (long)ip, tp->disp); in poke_int3_handler()
2236 * text_poke_bp_batch() -- update instructions on live kernel on SMP
2240 * Modify multi-byte instruction by using int3 breakpoint on SMP.
2245 * - For each entry in the vector:
2246 * - add a int3 trap to the address that will be patched
2247 * - sync cores
2248 * - For each entry in the vector:
2249 * - update all but the first byte of the patched range
2250 * - sync cores
2251 * - For each entry in the vector:
2252 * - replace the first byte (int3) by the first byte of
2254 * - sync cores
2269 * ensure reading a non-zero refcount provides up to date bp_desc data. in text_poke_bp_batch()
2308 if (len - INT3_INSN_SIZE > 0) { in text_poke_bp_batch()
2311 len - INT3_INSN_SIZE); in text_poke_bp_batch()
2321 len - INT3_INSN_SIZE); in text_poke_bp_batch()
2330 * - write INT3 byte in text_poke_bp_batch()
2331 * - IPI-SYNC in text_poke_bp_batch()
2332 * - write instruction tail in text_poke_bp_batch()
2337 * - emit RECORD_TEXT_POKE with the new instruction in text_poke_bp_batch()
2338 * - IPI-SYNC in text_poke_bp_batch()
2339 * - write first byte in text_poke_bp_batch()
2340 * - IPI-SYNC in text_poke_bp_batch()
2397 memcpy((void *)tp->text, opcode+i, len-i); in text_poke_loc_init()
2404 tp->rel_addr = addr - (void *)_stext; in text_poke_loc_init()
2405 tp->len = len; in text_poke_loc_init()
2406 tp->opcode = insn.opcode.bytes[0]; in text_poke_loc_init()
2412 tp->opcode = insn.opcode.bytes[1] - 0x10; in text_poke_loc_init()
2415 switch (tp->opcode) { in text_poke_loc_init()
2424 BUG_ON(tp->text[i] != INT3_INSN_OPCODE); in text_poke_loc_init()
2431 switch (tp->opcode) { in text_poke_loc_init()
2440 tp->disp = insn.immediate.value; in text_poke_loc_init()
2445 case 2: /* NOP2 -- emulate as JMP8+0 */ in text_poke_loc_init()
2447 tp->opcode = JMP8_INSN_OPCODE; in text_poke_loc_init()
2448 tp->disp = 0; in text_poke_loc_init()
2451 case 5: /* NOP5 -- emulate as JMP32+0 */ in text_poke_loc_init()
2453 tp->opcode = JMP32_INSN_OPCODE; in text_poke_loc_init()
2454 tp->disp = 0; in text_poke_loc_init()
2478 tp = &tp_vec[tp_vec_nr - 1]; in tp_order_fail()
2509 * text_poke_bp() -- update instructions on live kernel on SMP