Implement a ksplice_match_data_early option.
[ksplice.git] / kmodsrc / ksplice.h
blob7aa57f02b3b8602525e846735bd7e44ad6a91374
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 * @insn_addend: The part of the ELF addend resulting from quirks of
24 * the instruction one of whose operands is the relocation.
25 * For example, this is -4 on x86 pc-relative jumps.
26 * @target_addend: The rest of the ELF addend. This is equal to the offset
27 * against the symbol that the relocation refers to.
28 **/
29 struct ksplice_reloc {
30 unsigned long blank_addr;
31 struct ksplice_symbol *symbol;
32 const struct ksplice_reloc_howto *howto;
33 long insn_addend;
34 long target_addend;
37 enum ksplice_reloc_howto_type {
38 KSPLICE_HOWTO_RELOC,
39 KSPLICE_HOWTO_RELOC_PATCH,
40 KSPLICE_HOWTO_DATE,
41 KSPLICE_HOWTO_TIME,
42 KSPLICE_HOWTO_BUG,
43 KSPLICE_HOWTO_EXTABLE,
44 KSPLICE_HOWTO_SYMBOL,
47 /**
48 * struct ksplice_reloc_howto - Ksplice's relocation type information
49 * @type: The type of the relocation
50 * @pcrel: Is the relocation PC relative?
51 * @size: The size, in bytes, of the item to be relocated
52 * @dst_mask: Bitmask for which parts of the instruction or data are
53 * replaced with the relocated value
54 * (based on dst_mask from GNU BFD's reloc_howto_struct)
55 * @rightshift: The value the final relocation is shifted right by;
56 * used to drop unwanted data from the relocation
57 * (based on rightshift from GNU BFD's reloc_howto_struct)
58 * @signed_addend: Should the addend be interpreted as a signed value?
59 **/
60 struct ksplice_reloc_howto {
61 enum ksplice_reloc_howto_type type;
62 int pcrel;
63 int size;
64 long dst_mask;
65 unsigned int rightshift;
66 int signed_addend;
69 #if BITS_PER_LONG == 32
70 #define KSPLICE_CANARY 0x77777777UL
71 #elif BITS_PER_LONG == 64
72 #define KSPLICE_CANARY 0x7777777777777777UL
73 #endif /* BITS_PER_LONG */
75 /**
76 * struct ksplice_section - Ksplice's analogue of an ELF section
77 * @symbol: The ksplice_symbol associated with this section
78 * @size: The length, in bytes, of this section
79 * @address: The address of the section
80 * @flags: Flags indicating the type of the section, whether or
81 * not it has been matched, etc.
82 **/
83 struct ksplice_section {
84 struct ksplice_symbol *symbol;
85 unsigned long address;
86 unsigned long size;
87 unsigned int flags;
88 const unsigned char **match_map;
89 int unmatched;
91 #define KSPLICE_SECTION_TEXT 0x00000001
92 #define KSPLICE_SECTION_RODATA 0x00000002
93 #define KSPLICE_SECTION_DATA 0x00000004
94 #define KSPLICE_SECTION_STRING 0x00000008
95 #define KSPLICE_SECTION_MATCH_DATA_EARLY 0x00000100
96 #define KSPLICE_SECTION_MATCHED 0x10000000
98 #define MAX_TRAMPOLINE_SIZE 5
100 enum ksplice_patch_type {
101 KSPLICE_PATCH_TEXT,
102 KSPLICE_PATCH_DATA,
103 KSPLICE_PATCH_EXPORT,
107 * struct ksplice_patch - A replacement that Ksplice should perform
108 * @oldaddr: The address of the obsolete function or structure
109 * @repladdr: The address of the replacement function
110 * @type: The type of the ksplice patch
111 * @size: The size of the patch
112 * @contents: The bytes to be installed at oldaddr
113 * @vaddr The address of the page mapping used to write at oldaddr
114 * @saved: The bytes originally at oldaddr which were
115 * overwritten by the patch
117 struct ksplice_patch {
118 unsigned long oldaddr;
119 unsigned long repladdr;
120 enum ksplice_patch_type type;
121 unsigned int size;
122 void *contents;
123 /* private: */
124 void *vaddr;
125 void *saved;
128 #ifdef KSPLICE_STANDALONE
129 struct ksplice_system_map {
130 const char *label;
131 unsigned long nr_candidates;
132 const unsigned long *candidates;
134 #endif /* KSPLICE_STANDALONE */
136 #ifdef __KERNEL__
137 #include <linux/module.h>
138 #include <linux/stringify.h>
139 #include <linux/version.h>
141 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
142 /* 6e21828743247270d09a86756a0c11702500dbfb was after 2.6.18 */
143 #define bool _Bool
144 #define false 0
145 #define true 1
146 #endif /* LINUX_VERSION_CODE */
148 #if defined(CONFIG_PARAVIRT) && defined(CONFIG_X86_64) && \
149 LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) && \
150 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
151 /* Linux 2.6.25 and 2.6.26 apply paravirt replacements to the core
152 * kernel but not modules on x86-64. If we are patching the core
153 * kernel, we need to apply the same replacements to our update
154 * modules in order for run-pre matching to succeed.
156 #define KSPLICE_NEED_PARAINSTRUCTIONS 1
157 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
159 #define _PASTE(x, y) x##y
160 #define PASTE(x, y) _PASTE(x, y)
161 #define KSPLICE_UNIQ(s) PASTE(s##_, KSPLICE_MID)
162 #define KSPLICE_KID_UNIQ(s) PASTE(s##_, KSPLICE_KID)
163 #ifdef KSPLICE_STANDALONE
164 #define init_ksplice_mod_change KSPLICE_KID_UNIQ(init_ksplice_mod_change)
165 #define cleanup_ksplice_mod_change KSPLICE_KID_UNIQ(cleanup_ksplice_mod_change)
166 #endif
169 * struct ksplice_module_list_entry - A record of a ksplice_mod_change's target
170 * @target_mod_name: The name of the ksplice_mod_change's target module
171 * @new_code_mod_name: The name of the ksplice_mod_change's new_code module
172 * @applied: Whether the ksplice_mod_change was applied or not (this
173 * will be false for ksplice_mod_changes patching targets
174 * that are not loaded when the partial flag is set)
176 struct ksplice_module_list_entry {
177 const char *target_mod_name;
178 const char *new_code_mod_name;
179 const char *kid;
180 bool applied;
181 /* private: */
182 struct list_head update_list; /* list head for this is per-update */
183 struct list_head list; /* list head for this is global */
186 /* List of all ksplice modules and the module they patch */
187 extern struct list_head ksplice_modules;
189 /* There are two actions, apply and reverse */
190 #define KS_ACTIONS 2
191 enum ksplice_action {
192 KS_APPLY,
193 KS_REVERSE,
197 * struct ksplice_hooks - Hooks to be run during an action (apply or reverse)
198 * @pre: Runs before the action;
199 * may return nonzero to abort the action
200 * @check: Runs inside stop_machine before the action;
201 * may return nonzero to abort the action
202 * @intra: Runs inside stop_machine during the action
203 * @post: Runs after the action is successfully performed
204 * @fail: Runs if the action is aborted for any reason
206 struct ksplice_hooks {
207 const typeof(int (*)(void)) *pre, *pre_end, *check, *check_end;
208 const typeof(void (*)(void)) *intra, *intra_end, *post, *post_end,
209 *fail, *fail_end;
213 * struct ksplice_code - Ksplice metadata for an object
214 * @relocs: The Ksplice relocations for the object
215 * @symbols: The Ksplice symbols for the object
216 * @sections: The Ksplice sections for the object
218 struct ksplice_code {
219 struct ksplice_reloc *relocs, *relocs_end;
220 struct ksplice_section *sections, *sections_end;
221 struct ksplice_symbol *symbols, *symbols_end;
222 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
223 struct paravirt_patch_site *parainstructions, *parainstructions_end;
224 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
225 #ifdef KSPLICE_STANDALONE
226 struct ksplice_system_map *system_map, *system_map_end;
227 #endif /* KSPLICE_STANDALONE */
231 * struct ksplice_mod_change - Data for one module modified by a Ksplice update
232 * @name: The name of the new_code module for the change
233 * @kid: The Ksplice unique identifier for the change
234 * @target_name: The name of the module modified by the change
235 * @new_code_mod: The new_code module for the change
236 * @old_code: The old code for run-pre matching
237 * @new_code: The new code to switch to
238 * @patches: The function replacements in the change
239 * @patches_end: The end pointer for patches array
240 * @hooks: Hooks to be run during apply and reverse
241 * @update: The atomic update the change is part of
242 * @target: The module modified by the change
243 * @safety_records: The ranges of addresses that must not be on a
244 * kernel stack for the patch to apply safely
246 struct ksplice_mod_change {
247 const char *name;
248 const char *kid;
249 const char *target_name;
250 #ifdef KSPLICE_STANDALONE
251 unsigned long map_printk;
252 #endif /* KSPLICE_STANDALONE */
253 struct module *new_code_mod;
254 struct ksplice_code old_code, new_code;
255 struct ksplice_patch *patches, *patches_end;
256 struct ksplice_hooks hooks[KS_ACTIONS];
257 /* private: */
258 struct update *update;
259 struct module *target;
260 struct list_head temp_labelvals;
261 struct list_head safety_records;
262 struct list_head list;
266 int init_ksplice_mod_change(struct ksplice_mod_change *change);
268 void cleanup_ksplice_mod_change(struct ksplice_mod_change *change);
270 #endif /* __KERNEL__ */