Makefile.ksplice: Rewrite ksplice-cow-check as a wrapper function.
[ksplice.git] / kmodsrc / ksplice.h
blob6f1c1fa746869de07ce72266e8b256c4a4c70e7e
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_MATCHED 0x10000000
97 #define MAX_TRAMPOLINE_SIZE 5
99 enum ksplice_patch_type {
100 KSPLICE_PATCH_TEXT,
101 KSPLICE_PATCH_DATA,
102 KSPLICE_PATCH_EXPORT,
106 * struct ksplice_patch - A replacement that Ksplice should perform
107 * @oldaddr: The address of the obsolete function or structure
108 * @repladdr: The address of the replacement function
109 * @type: The type of the ksplice patch
110 * @size: The size of the patch
111 * @contents: The bytes to be installed at oldaddr
112 * @vaddr The address of the page mapping used to write at oldaddr
113 * @saved: The bytes originally at oldaddr which were
114 * overwritten by the patch
116 struct ksplice_patch {
117 unsigned long oldaddr;
118 unsigned long repladdr;
119 enum ksplice_patch_type type;
120 unsigned int size;
121 void *contents;
122 /* private: */
123 void *vaddr;
124 void *saved;
127 #ifdef KSPLICE_STANDALONE
128 struct ksplice_system_map {
129 const char *label;
130 unsigned long nr_candidates;
131 const unsigned long *candidates;
133 #endif /* KSPLICE_STANDALONE */
135 #ifdef __KERNEL__
136 #include <linux/module.h>
137 #include <linux/stringify.h>
138 #include <linux/version.h>
140 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
141 /* 6e21828743247270d09a86756a0c11702500dbfb was after 2.6.18 */
142 #define bool _Bool
143 #define false 0
144 #define true 1
145 #endif /* LINUX_VERSION_CODE */
147 #if defined(CONFIG_PARAVIRT) && defined(CONFIG_X86_64) && \
148 LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) && \
149 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
150 /* Linux 2.6.25 and 2.6.26 apply paravirt replacements to the core
151 * kernel but not modules on x86-64. If we are patching the core
152 * kernel, we need to apply the same replacements to our update
153 * modules in order for run-pre matching to succeed.
155 #define KSPLICE_NEED_PARAINSTRUCTIONS 1
156 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
158 #define _PASTE(x, y) x##y
159 #define PASTE(x, y) _PASTE(x, y)
160 #define KSPLICE_UNIQ(s) PASTE(s##_, KSPLICE_MID)
161 #define KSPLICE_KID_UNIQ(s) PASTE(s##_, KSPLICE_KID)
162 #ifdef KSPLICE_STANDALONE
163 #define init_ksplice_mod_change KSPLICE_KID_UNIQ(init_ksplice_mod_change)
164 #define cleanup_ksplice_mod_change KSPLICE_KID_UNIQ(cleanup_ksplice_mod_change)
165 #endif
168 * struct ksplice_module_list_entry - A record of a ksplice_mod_change's target
169 * @target_mod_name: The name of the ksplice_mod_change's target module
170 * @new_code_mod_name: The name of the ksplice_mod_change's new_code module
171 * @applied: Whether the ksplice_mod_change was applied or not (this
172 * will be false for ksplice_mod_changes patching targets
173 * that are not loaded when the partial flag is set)
175 struct ksplice_module_list_entry {
176 const char *target_mod_name;
177 const char *new_code_mod_name;
178 const char *kid;
179 bool applied;
180 /* private: */
181 struct list_head update_list; /* list head for this is per-update */
182 struct list_head list; /* list head for this is global */
185 /* List of all ksplice modules and the module they patch */
186 extern struct list_head ksplice_modules;
188 /* There are two actions, apply and reverse */
189 #define KS_ACTIONS 2
190 enum ksplice_action {
191 KS_APPLY,
192 KS_REVERSE,
196 * struct ksplice_hooks - Hooks to be run during an action (apply or reverse)
197 * @pre: Runs before the action;
198 * may return nonzero to abort the action
199 * @check: Runs inside stop_machine before the action;
200 * may return nonzero to abort the action
201 * @intra: Runs inside stop_machine during the action
202 * @post: Runs after the action is successfully performed
203 * @fail: Runs if the action is aborted for any reason
205 struct ksplice_hooks {
206 const typeof(int (*)(void)) *pre, *pre_end, *check, *check_end;
207 const typeof(void (*)(void)) *intra, *intra_end, *post, *post_end,
208 *fail, *fail_end;
212 * struct ksplice_code - Ksplice metadata for an object
213 * @relocs: The Ksplice relocations for the object
214 * @symbols: The Ksplice symbols for the object
215 * @sections: The Ksplice sections for the object
217 struct ksplice_code {
218 struct ksplice_reloc *relocs, *relocs_end;
219 struct ksplice_section *sections, *sections_end;
220 struct ksplice_symbol *symbols, *symbols_end;
221 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
222 struct paravirt_patch_site *parainstructions, *parainstructions_end;
223 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
224 #ifdef KSPLICE_STANDALONE
225 struct ksplice_system_map *system_map, *system_map_end;
226 #endif /* KSPLICE_STANDALONE */
230 * struct ksplice_mod_change - Data for one module modified by a Ksplice update
231 * @name: The name of the new_code module for the change
232 * @kid: The Ksplice unique identifier for the change
233 * @target_name: The name of the module modified by the change
234 * @new_code_mod: The new_code module for the change
235 * @old_code: The old code for run-pre matching
236 * @new_code: The new code to switch to
237 * @patches: The function replacements in the change
238 * @patches_end: The end pointer for patches array
239 * @hooks: Hooks to be run during apply and reverse
240 * @update: The atomic update the change is part of
241 * @target: The module modified by the change
242 * @safety_records: The ranges of addresses that must not be on a
243 * kernel stack for the patch to apply safely
245 struct ksplice_mod_change {
246 const char *name;
247 const char *kid;
248 const char *target_name;
249 #ifdef KSPLICE_STANDALONE
250 unsigned long map_printk;
251 #endif /* KSPLICE_STANDALONE */
252 struct module *new_code_mod;
253 struct ksplice_code old_code, new_code;
254 struct ksplice_patch *patches, *patches_end;
255 struct ksplice_hooks hooks[KS_ACTIONS];
256 /* private: */
257 struct update *update;
258 struct module *target;
259 struct list_head temp_labelvals;
260 struct list_head safety_records;
261 struct list_head list;
265 int init_ksplice_mod_change(struct ksplice_mod_change *change);
267 void cleanup_ksplice_mod_change(struct ksplice_mod_change *change);
269 #endif /* __KERNEL__ */