Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * jump label x86 support |
| 3 | * |
| 4 | * Copyright (C) 2009 Jason Baron <jbaron@redhat.com> |
| 5 | * |
| 6 | */ |
| 7 | #include <linux/jump_label.h> |
| 8 | #include <linux/memory.h> |
| 9 | #include <linux/uaccess.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/jhash.h> |
| 13 | #include <linux/cpu.h> |
| 14 | #include <asm/kprobes.h> |
| 15 | #include <asm/alternative.h> |
| 16 | |
| 17 | #ifdef HAVE_JUMP_LABEL |
| 18 | |
| 19 | union jump_code_union { |
| 20 | char code[JUMP_LABEL_NOP_SIZE]; |
| 21 | struct { |
| 22 | char jump; |
| 23 | int offset; |
| 24 | } __attribute__((packed)); |
| 25 | }; |
| 26 | |
Jeremy Fitzhardinge | e71a5be | 2011-09-29 11:11:09 -0700 | [diff] [blame] | 27 | static void __jump_label_transform(struct jump_entry *entry, |
| 28 | enum jump_label_type type, |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 29 | void *(*poker)(void *, const void *, size_t), |
| 30 | int init) |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 31 | { |
| 32 | union jump_code_union code; |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 33 | const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 34 | |
| 35 | if (type == JUMP_LABEL_ENABLE) { |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 36 | /* |
| 37 | * We are enabling this jump label. If it is not a nop |
| 38 | * then something must have gone wrong. |
| 39 | */ |
| 40 | BUG_ON(memcmp((void *)entry->code, ideal_nop, 5) != 0); |
| 41 | |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 42 | code.jump = 0xe9; |
| 43 | code.offset = entry->target - |
| 44 | (entry->code + JUMP_LABEL_NOP_SIZE); |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 45 | } else { |
| 46 | /* |
| 47 | * We are disabling this jump label. If it is not what |
| 48 | * we think it is, then something must have gone wrong. |
| 49 | * If this is the first initialization call, then we |
| 50 | * are converting the default nop to the ideal nop. |
| 51 | */ |
| 52 | if (init) { |
| 53 | const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; |
| 54 | BUG_ON(memcmp((void *)entry->code, default_nop, 5) != 0); |
| 55 | } else { |
| 56 | code.jump = 0xe9; |
| 57 | code.offset = entry->target - |
| 58 | (entry->code + JUMP_LABEL_NOP_SIZE); |
| 59 | BUG_ON(memcmp((void *)entry->code, &code, 5) != 0); |
| 60 | } |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 61 | memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE); |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 62 | } |
Jeremy Fitzhardinge | e71a5be | 2011-09-29 11:11:09 -0700 | [diff] [blame] | 63 | |
| 64 | (*poker)((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE); |
| 65 | } |
| 66 | |
| 67 | void arch_jump_label_transform(struct jump_entry *entry, |
| 68 | enum jump_label_type type) |
| 69 | { |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 70 | get_online_cpus(); |
| 71 | mutex_lock(&text_mutex); |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 72 | __jump_label_transform(entry, type, text_poke_smp, 0); |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 73 | mutex_unlock(&text_mutex); |
| 74 | put_online_cpus(); |
| 75 | } |
| 76 | |
Steven Rostedt | 11570da | 2012-01-26 18:16:15 -0500 | [diff] [blame] | 77 | static enum { |
| 78 | JL_STATE_START, |
| 79 | JL_STATE_NO_UPDATE, |
| 80 | JL_STATE_UPDATE, |
| 81 | } jlstate __initdata_or_module = JL_STATE_START; |
| 82 | |
Peter Zijlstra | 9cdbe1c | 2011-12-06 17:27:29 +0100 | [diff] [blame] | 83 | __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, |
Jeremy Fitzhardinge | e71a5be | 2011-09-29 11:11:09 -0700 | [diff] [blame] | 84 | enum jump_label_type type) |
| 85 | { |
Steven Rostedt | 11570da | 2012-01-26 18:16:15 -0500 | [diff] [blame] | 86 | /* |
| 87 | * This function is called at boot up and when modules are |
| 88 | * first loaded. Check if the default nop, the one that is |
| 89 | * inserted at compile time, is the ideal nop. If it is, then |
| 90 | * we do not need to update the nop, and we can leave it as is. |
| 91 | * If it is not, then we need to update the nop to the ideal nop. |
| 92 | */ |
| 93 | if (jlstate == JL_STATE_START) { |
| 94 | const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; |
| 95 | const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; |
| 96 | |
| 97 | if (memcmp(ideal_nop, default_nop, 5) != 0) |
| 98 | jlstate = JL_STATE_UPDATE; |
| 99 | else |
| 100 | jlstate = JL_STATE_NO_UPDATE; |
| 101 | } |
| 102 | if (jlstate == JL_STATE_UPDATE) |
Steven Rostedt | 9c85f3b | 2012-01-26 18:38:07 -0500 | [diff] [blame^] | 103 | __jump_label_transform(entry, type, text_poke_early, 1); |
Jeremy Fitzhardinge | e71a5be | 2011-09-29 11:11:09 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jason Baron | d9f5ab7 | 2010-09-17 11:09:22 -0400 | [diff] [blame] | 106 | #endif |