1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d9f5ab7bSJason Baron /* 3d9f5ab7bSJason Baron * jump label x86 support 4d9f5ab7bSJason Baron * 5d9f5ab7bSJason Baron * Copyright (C) 2009 Jason Baron <jbaron@redhat.com> 6d9f5ab7bSJason Baron * 7d9f5ab7bSJason Baron */ 8d9f5ab7bSJason Baron #include <linux/jump_label.h> 9d9f5ab7bSJason Baron #include <linux/memory.h> 10d9f5ab7bSJason Baron #include <linux/uaccess.h> 11d9f5ab7bSJason Baron #include <linux/module.h> 12d9f5ab7bSJason Baron #include <linux/list.h> 13d9f5ab7bSJason Baron #include <linux/jhash.h> 14d9f5ab7bSJason Baron #include <linux/cpu.h> 15d9f5ab7bSJason Baron #include <asm/kprobes.h> 16d9f5ab7bSJason Baron #include <asm/alternative.h> 1735de5b06SAndy Lutomirski #include <asm/text-patching.h> 18d9f5ab7bSJason Baron 19d9f5ab7bSJason Baron union jump_code_union { 20d9f5ab7bSJason Baron char code[JUMP_LABEL_NOP_SIZE]; 21d9f5ab7bSJason Baron struct { 22d9f5ab7bSJason Baron char jump; 23d9f5ab7bSJason Baron int offset; 24d9f5ab7bSJason Baron } __attribute__((packed)); 25d9f5ab7bSJason Baron }; 26d9f5ab7bSJason Baron 27fb40d7a8SSteven Rostedt static void bug_at(unsigned char *ip, int line) 28fb40d7a8SSteven Rostedt { 29fb40d7a8SSteven Rostedt /* 30fb40d7a8SSteven Rostedt * The location is not an op that we were expecting. 31fb40d7a8SSteven Rostedt * Something went wrong. Crash the box, as something could be 32fb40d7a8SSteven Rostedt * corrupting the kernel. 33fb40d7a8SSteven Rostedt */ 346e03f66cSAndy Shevchenko pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph) %d\n", ip, ip, ip, line); 35fb40d7a8SSteven Rostedt BUG(); 36fb40d7a8SSteven Rostedt } 37fb40d7a8SSteven Rostedt 38*4cc6620bSDaniel Bristot de Oliveira static void __jump_label_set_jump_code(struct jump_entry *entry, 39e71a5be1SJeremy Fitzhardinge enum jump_label_type type, 40*4cc6620bSDaniel Bristot de Oliveira union jump_code_union *code, 419c85f3bdSSteven Rostedt int init) 42d9f5ab7bSJason Baron { 43a8fab074SHannes Frederic Sowa const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; 449c85f3bdSSteven Rostedt const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; 45*4cc6620bSDaniel Bristot de Oliveira const void *expect; 469fc0f798SArd Biesheuvel int line; 479fc0f798SArd Biesheuvel 48*4cc6620bSDaniel Bristot de Oliveira code->jump = 0xe9; 49*4cc6620bSDaniel Bristot de Oliveira code->offset = jump_entry_target(entry) - 509fc0f798SArd Biesheuvel (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE); 51d9f5ab7bSJason Baron 52a8fab074SHannes Frederic Sowa if (init) { 539fc0f798SArd Biesheuvel expect = default_nop; line = __LINE__; 54*4cc6620bSDaniel Bristot de Oliveira } else if (type == JUMP_LABEL_JMP) { 559fc0f798SArd Biesheuvel expect = ideal_nop; line = __LINE__; 569c85f3bdSSteven Rostedt } else { 57*4cc6620bSDaniel Bristot de Oliveira expect = code->code; line = __LINE__; 589c85f3bdSSteven Rostedt } 59e71a5be1SJeremy Fitzhardinge 609fc0f798SArd Biesheuvel if (memcmp((void *)jump_entry_code(entry), expect, JUMP_LABEL_NOP_SIZE)) 619fc0f798SArd Biesheuvel bug_at((void *)jump_entry_code(entry), line); 629fc0f798SArd Biesheuvel 63*4cc6620bSDaniel Bristot de Oliveira if (type == JUMP_LABEL_NOP) 64*4cc6620bSDaniel Bristot de Oliveira memcpy(code, ideal_nop, JUMP_LABEL_NOP_SIZE); 65*4cc6620bSDaniel Bristot de Oliveira } 66*4cc6620bSDaniel Bristot de Oliveira 67*4cc6620bSDaniel Bristot de Oliveira static void __ref __jump_label_transform(struct jump_entry *entry, 68*4cc6620bSDaniel Bristot de Oliveira enum jump_label_type type, 69*4cc6620bSDaniel Bristot de Oliveira int init) 70*4cc6620bSDaniel Bristot de Oliveira { 71*4cc6620bSDaniel Bristot de Oliveira union jump_code_union code; 72*4cc6620bSDaniel Bristot de Oliveira 73*4cc6620bSDaniel Bristot de Oliveira __jump_label_set_jump_code(entry, type, &code, init); 74*4cc6620bSDaniel Bristot de Oliveira 7551b2c07bSJiri Kosina /* 76bb0a008dSNadav Amit * As long as only a single processor is running and the code is still 77bb0a008dSNadav Amit * not marked as RO, text_poke_early() can be used; Checking that 78bb0a008dSNadav Amit * system_state is SYSTEM_BOOTING guarantees it. It will be set to 79bb0a008dSNadav Amit * SYSTEM_SCHEDULING before other cores are awaken and before the 80bb0a008dSNadav Amit * code is write-protected. 8151b2c07bSJiri Kosina * 8251b2c07bSJiri Kosina * At the time the change is being done, just ignore whether we 8351b2c07bSJiri Kosina * are doing nop -> jump or jump -> nop transition, and assume 8451b2c07bSJiri Kosina * always nop being the 'currently valid' instruction 8551b2c07bSJiri Kosina */ 86bb0a008dSNadav Amit if (init || system_state == SYSTEM_BOOTING) { 87*4cc6620bSDaniel Bristot de Oliveira text_poke_early((void *)jump_entry_code(entry), &code, 889fc0f798SArd Biesheuvel JUMP_LABEL_NOP_SIZE); 899fc0f798SArd Biesheuvel return; 909fc0f798SArd Biesheuvel } 919fc0f798SArd Biesheuvel 92*4cc6620bSDaniel Bristot de Oliveira text_poke_bp((void *)jump_entry_code(entry), &code, JUMP_LABEL_NOP_SIZE, 939fc0f798SArd Biesheuvel (void *)jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE); 94e71a5be1SJeremy Fitzhardinge } 95e71a5be1SJeremy Fitzhardinge 96e71a5be1SJeremy Fitzhardinge void arch_jump_label_transform(struct jump_entry *entry, 97e71a5be1SJeremy Fitzhardinge enum jump_label_type type) 98e71a5be1SJeremy Fitzhardinge { 99d9f5ab7bSJason Baron mutex_lock(&text_mutex); 100bb0a008dSNadav Amit __jump_label_transform(entry, type, 0); 101d9f5ab7bSJason Baron mutex_unlock(&text_mutex); 102d9f5ab7bSJason Baron } 103d9f5ab7bSJason Baron 10411570da1SSteven Rostedt static enum { 10511570da1SSteven Rostedt JL_STATE_START, 10611570da1SSteven Rostedt JL_STATE_NO_UPDATE, 10711570da1SSteven Rostedt JL_STATE_UPDATE, 10811570da1SSteven Rostedt } jlstate __initdata_or_module = JL_STATE_START; 10911570da1SSteven Rostedt 1109cdbe1cbSPeter Zijlstra __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, 111e71a5be1SJeremy Fitzhardinge enum jump_label_type type) 112e71a5be1SJeremy Fitzhardinge { 11311570da1SSteven Rostedt /* 11411570da1SSteven Rostedt * This function is called at boot up and when modules are 11511570da1SSteven Rostedt * first loaded. Check if the default nop, the one that is 11611570da1SSteven Rostedt * inserted at compile time, is the ideal nop. If it is, then 11711570da1SSteven Rostedt * we do not need to update the nop, and we can leave it as is. 11811570da1SSteven Rostedt * If it is not, then we need to update the nop to the ideal nop. 11911570da1SSteven Rostedt */ 12011570da1SSteven Rostedt if (jlstate == JL_STATE_START) { 12111570da1SSteven Rostedt const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; 12211570da1SSteven Rostedt const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; 12311570da1SSteven Rostedt 12411570da1SSteven Rostedt if (memcmp(ideal_nop, default_nop, 5) != 0) 12511570da1SSteven Rostedt jlstate = JL_STATE_UPDATE; 12611570da1SSteven Rostedt else 12711570da1SSteven Rostedt jlstate = JL_STATE_NO_UPDATE; 12811570da1SSteven Rostedt } 12911570da1SSteven Rostedt if (jlstate == JL_STATE_UPDATE) 130bb0a008dSNadav Amit __jump_label_transform(entry, type, 1); 131e71a5be1SJeremy Fitzhardinge } 132