Write deleted patches for global symbols removed from the middle of a span.
[ksplice.git] / kmodsrc / ksplice.h
blobff62ec358d387533bc3947a1bda23b7ef341c2bc
1 #include <linux/types.h>
3 /**
4 * struct ksplice_symbol - Ksplice's analogue of an ELF symbol
5 * @name: The ELF name of the symbol
6 * @label: A unique Ksplice name for the symbol
7 * @candidate_vals: A linked list of possible values for the symbol, or NULL
8 * @value: The value of the symbol (valid when vals is NULL)
9 **/
10 struct ksplice_symbol {
11 const char *name;
12 const char *label;
13 /* private: */
14 struct list_head *candidate_vals;
15 unsigned long value;
18 /**
19 * struct ksplice_reloc - Ksplice's analogue of an ELF relocation
20 * @blank_addr: The address of the relocation's storage unit
21 * @symbol: The ksplice_symbol associated with this relocation
22 * @howto: The information regarding the relocation type
23 * @addend: The ELF addend of the relocation
24 **/
25 struct ksplice_reloc {
26 unsigned long blank_addr;
27 struct ksplice_symbol *symbol;
28 const struct ksplice_reloc_howto *howto;
29 long insn_addend;
30 long target_addend;
33 enum ksplice_reloc_howto_type {
34 KSPLICE_HOWTO_RELOC,
35 KSPLICE_HOWTO_RELOC_PATCH,
36 KSPLICE_HOWTO_DATE,
37 KSPLICE_HOWTO_TIME,
38 KSPLICE_HOWTO_BUG,
39 KSPLICE_HOWTO_EXTABLE,
40 KSPLICE_HOWTO_SYMBOL,
43 /**
44 * struct ksplice_reloc_howto - Ksplice's relocation type information
45 * @type: The type of the relocation
46 * @pcrel: Is the relocation PC relative?
47 * @size: The size, in bytes, of the item to be relocated
48 * @dst_mask: Bitmask for which parts of the instruction or data are
49 * replaced with the relocated value
50 * (based on dst_mask from GNU BFD's reloc_howto_struct)
51 * @rightshift: The value the final relocation is shifted right by;
52 * used to drop unwanted data from the relocation
53 * (based on rightshift from GNU BFD's reloc_howto_struct)
54 * @signed_addend: Should the addend be interpreted as a signed value?
55 **/
56 struct ksplice_reloc_howto {
57 enum ksplice_reloc_howto_type type;
58 int pcrel;
59 int size;
60 long dst_mask;
61 unsigned int rightshift;
62 int signed_addend;
65 #if BITS_PER_LONG == 32
66 #define KSPLICE_CANARY 0x77777777UL
67 #elif BITS_PER_LONG == 64
68 #define KSPLICE_CANARY 0x7777777777777777UL
69 #endif /* BITS_PER_LONG */
71 /**
72 * struct ksplice_section - Ksplice's analogue of an ELF section
73 * @symbol: The ksplice_symbol associated with this section
74 * @size: The length, in bytes, of this section
75 * @address: The address of the section
76 * @flags: Flags indicating the type of the section, whether or
77 * not it has been matched, etc.
78 **/
79 struct ksplice_section {
80 struct ksplice_symbol *symbol;
81 unsigned long address;
82 unsigned long size;
83 unsigned int flags;
84 const unsigned char **match_map;
86 #define KSPLICE_SECTION_TEXT 0x00000001
87 #define KSPLICE_SECTION_RODATA 0x00000002
88 #define KSPLICE_SECTION_DATA 0x00000004
89 #define KSPLICE_SECTION_STRING 0x00000008
90 #define KSPLICE_SECTION_MATCHED 0x10000000
92 #define MAX_TRAMPOLINE_SIZE 5
94 enum ksplice_patch_type {
95 KSPLICE_PATCH_TEXT,
96 KSPLICE_PATCH_DATA,
97 KSPLICE_PATCH_EXPORT,
101 * struct ksplice_patch - A replacement that Ksplice should perform
102 * @oldaddr: The address of the obsolete function or structure
103 * @repladdr: The address of the replacement function
104 * @type: The type of the ksplice patch
105 * @size: The size of the patch
106 * @contents: The bytes to be installed at oldaddr
107 * @vaddr The address of the page mapping used to write at oldaddr
108 * @saved: The bytes originally at oldaddr which were
109 * overwritten by the patch
111 struct ksplice_patch {
112 unsigned long oldaddr;
113 unsigned long repladdr;
114 enum ksplice_patch_type type;
115 unsigned int size;
116 void *contents;
117 /* private: */
118 void *vaddr;
119 void *saved;
122 #ifdef KSPLICE_STANDALONE
123 struct ksplice_system_map {
124 const char *label;
125 unsigned long nr_candidates;
126 const unsigned long *candidates;
128 #endif /* KSPLICE_STANDALONE */
130 #ifdef __KERNEL__
131 #include <linux/module.h>
132 #include <linux/stringify.h>
133 #include <linux/version.h>
135 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
136 /* 6e21828743247270d09a86756a0c11702500dbfb was after 2.6.18 */
137 #define bool _Bool
138 #define false 0
139 #define true 1
140 #endif /* LINUX_VERSION_CODE */
142 #if defined(CONFIG_PARAVIRT) && defined(CONFIG_X86_64) && \
143 LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) && \
144 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
145 /* Linux 2.6.25 and 2.6.26 apply paravirt replacements to the core
146 * kernel but not modules on x86-64. If we are patching the core
147 * kernel, we need to apply the same replacements to our update
148 * modules in order for run-pre matching to succeed.
150 #define KSPLICE_NEED_PARAINSTRUCTIONS 1
151 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
153 #define _PASTE(x, y) x##y
154 #define PASTE(x, y) _PASTE(x, y)
155 #define KSPLICE_UNIQ(s) PASTE(s##_, KSPLICE_MID)
156 #define KSPLICE_KID_UNIQ(s) PASTE(s##_, KSPLICE_KID)
157 #ifdef KSPLICE_STANDALONE
158 #define init_ksplice_mod_change KSPLICE_KID_UNIQ(init_ksplice_mod_change)
159 #define cleanup_ksplice_mod_change KSPLICE_KID_UNIQ(cleanup_ksplice_mod_change)
160 #endif
163 * struct ksplice_module_list_entry - A record of a ksplice_mod_change's target
164 * @target_mod_name: The name of the ksplice_mod_change's target module
165 * @new_code_mod_name: The name of the ksplice_mod_change's new_code module
166 * @applied: Whether the ksplice_mod_change was applied or not (this
167 * will be false for ksplice_mod_changes patching targets
168 * that are not loaded when the partial flag is set)
170 struct ksplice_module_list_entry {
171 const char *target_mod_name;
172 const char *new_code_mod_name;
173 const char *kid;
174 bool applied;
175 /* private: */
176 struct list_head update_list; /* list head for this is per-update */
177 struct list_head list; /* list head for this is global */
180 /* List of all ksplice modules and the module they patch */
181 extern struct list_head ksplice_modules;
183 /* There are two actions, apply and reverse */
184 #define KS_ACTIONS 2
185 enum ksplice_action {
186 KS_APPLY,
187 KS_REVERSE,
191 * struct ksplice_hooks - Hooks to be run during an action (apply or reverse)
192 * @pre: Runs before the action;
193 * may return nonzero to abort the action
194 * @check: Runs inside stop_machine before the action;
195 * may return nonzero to abort the action
196 * @intra: Runs inside stop_machine during the action
197 * @post: Runs after the action is successfully performed
198 * @fail: Runs if the action is aborted for any reason
200 struct ksplice_hooks {
201 const typeof(int (*)(void)) *pre, *pre_end, *check, *check_end;
202 const typeof(void (*)(void)) *intra, *intra_end, *post, *post_end,
203 *fail, *fail_end;
207 * struct ksplice_code - Ksplice metadata for an object
208 * @relocs: The Ksplice relocations for the object
209 * @symbols: The Ksplice symbols for the object
210 * @sections: The Ksplice sections for the object
212 struct ksplice_code {
213 struct ksplice_reloc *relocs, *relocs_end;
214 struct ksplice_section *sections, *sections_end;
215 struct ksplice_symbol *symbols, *symbols_end;
216 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
217 struct paravirt_patch_site *parainstructions, *parainstructions_end;
218 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
219 #ifdef KSPLICE_STANDALONE
220 struct ksplice_system_map *system_map, *system_map_end;
221 #endif /* KSPLICE_STANDALONE */
225 * struct ksplice_mod_change - Data for one module modified by a Ksplice update
226 * @name: The name of the new_code module for the change
227 * @kid: The Ksplice unique identifier for the change
228 * @target_name: The name of the module modified by the change
229 * @new_code_mod: The new_code module for the change
230 * @old_code: The old code for run-pre matching
231 * @new_code: The new code to switch to
232 * @patches: The function replacements in the change
233 * @patches_end: The end pointer for patches array
234 * @hooks: Hooks to be run during apply and reverse
235 * @update: The atomic update the change is part of
236 * @target: The module modified by the change
237 * @safety_records: The ranges of addresses that must not be on a
238 * kernel stack for the patch to apply safely
240 struct ksplice_mod_change {
241 const char *name;
242 const char *kid;
243 const char *target_name;
244 #ifdef KSPLICE_STANDALONE
245 unsigned long map_printk;
246 #endif /* KSPLICE_STANDALONE */
247 struct module *new_code_mod;
248 struct ksplice_code old_code, new_code;
249 struct ksplice_patch *patches, *patches_end;
250 struct ksplice_hooks hooks[KS_ACTIONS];
251 /* private: */
252 struct update *update;
253 struct module *target;
254 struct list_head temp_labelvals;
255 struct list_head safety_records;
256 struct list_head list;
260 int init_ksplice_mod_change(struct ksplice_mod_change *change);
262 void cleanup_ksplice_mod_change(struct ksplice_mod_change *change);
264 #endif /* __KERNEL__ */