undo: remove *_old modules early, before reversing
[ksplice.git] / ksplice-patch / ksplice-patch.h
blobc5ab62f97b7a0ece44dcac1ea039f3692112d64a
1 #ifndef _KSPLICE_PATCH_H
2 #define _KSPLICE_PATCH_H
4 enum ksplice_option_type {
5 KSPLICE_OPTION_ASSUME_RODATA,
6 };
8 struct ksplice_option {
9 enum ksplice_option_type type;
10 const void *target;
13 #ifdef __KERNEL__
14 #ifndef __used
15 #define __used __attribute_used__
16 #endif
18 #define ksplice_call_int(name, fn) \
19 static typeof(int (*)(void)) __ksplice_##name##_##fn __used \
20 __attribute__((__section__(".ksplice_call_" #name))) = fn
22 #define ksplice_call_void(name, fn) \
23 static typeof(void (*)(void)) __ksplice_##name##_##fn __used \
24 __attribute__((__section__(".ksplice_call_" #name))) = fn
26 #define ksplice_pre_apply(fn) ksplice_call_int(pre_apply, fn)
27 #define ksplice_check_apply(fn) ksplice_call_int(check_apply, fn)
28 #define ksplice_apply(fn) ksplice_call_void(apply, fn)
29 #define ksplice_post_apply(fn) ksplice_call_void(post_apply, fn)
30 #define ksplice_fail_apply(fn) ksplice_call_void(fail_apply, fn)
32 #define ksplice_pre_reverse(fn) ksplice_call_int(pre_reverse, fn)
33 #define ksplice_check_reverse(fn) ksplice_call_int(check_reverse, fn)
34 #define ksplice_reverse(fn) ksplice_call_void(reverse, fn)
35 #define ksplice_post_reverse(fn) ksplice_call_void(post_reverse, fn)
36 #define ksplice_fail_reverse(fn) ksplice_call_void(fail_reverse, fn)
38 #define ksplice_assume_rodata(obj) \
39 const struct ksplice_option \
40 __used __attribute__((__section__(".ksplice_options"))) \
41 assume_rodata_##obj = { \
42 .type = KSPLICE_OPTION_ASSUME_RODATA, \
43 .target = (const void *)&obj, \
46 int init_shadow_field_type(int *shadow_key, typeof(GFP_KERNEL) gfp_flags);
47 void *init_shadow_field(int *shadow_key, void *obj, int size,
48 typeof(GFP_KERNEL) gfp_flags);
49 void cleanup_shadow_field(int *shadow_key, void *obj);
50 void *get_shadow_field(int *shadow_key, void *obj);
51 void cleanup_shadow_field_type(int *shadow_key);
54 #define __DEFINE_SHADOW_FIELD(base_type, field_type, gfp_flags, \
55 init_field_type_fn, init_field_fn, get_field_fn, \
56 make_field_fn, cleanup_field_fn, \
57 cleanup_field_type_fn, shadow_key, init_field) \
58 static int shadow_key = 0; \
59 int init_field_type_fn(void) \
60 { \
61 return init_shadow_field_type(&shadow_key, gfp_flags); \
62 } \
63 field_type *init_field_fn(base_type *obj, typeof(GFP_KERNEL) flags) \
64 { \
65 field_type *data = init_shadow_field(&shadow_key, (void *)obj, \
66 sizeof(*data), flags); \
67 if (data != NULL) \
68 init_field(data); \
69 return data; \
70 } \
71 void cleanup_field_fn(base_type *obj) \
72 { \
73 cleanup_shadow_field(&shadow_key, obj); \
74 } \
75 field_type *get_field_fn(base_type *obj) \
76 { \
77 return get_shadow_field(&shadow_key, obj); \
78 } \
79 field_type *make_field_fn(base_type *obj, typeof(GFP_KERNEL) flags) \
80 { \
81 void *data = get_shadow_field(&shadow_key, (void *)obj); \
82 if (data == NULL) \
83 data = init_field_fn(obj, flags); \
84 return data; \
85 } \
86 void cleanup_field_type_fn(void) \
87 { \
88 return cleanup_shadow_field_type(&shadow_key); \
89 } \
90 struct eat_trailing_semicolon
92 #define DEFINE_SHADOW_FIELD(base_type, field_type, gfp_flags, name, init_field) \
93 __DEFINE_SHADOW_FIELD(base_type, field_type, gfp_flags, \
94 init_##name##_shadows, init_##name##_shadow, \
95 get_##name##_shadow, make_##name##_shadow, \
96 cleanup_##name##_shadow, cleanup_##name##_shadows,\
97 shadow_key_##name, init_field); \
98 ksplice_check_apply(init_##name##_shadows); \
99 ksplice_post_reverse(cleanup_##name##_shadows); \
100 ksplice_fail_apply(cleanup_##name##_shadows)
102 #define __DECLARE_SHADOW_FIELD(base_type, field_type, init_field_type_fn, \
103 init_field_fn, get_field_fn, make_field_fn, \
104 cleanup_field_fn, cleanup_field_type_fn) \
105 int init_field_type_fn(void); \
106 field_type *init_field_fn(base_type *obj, typeof(GFP_KERNEL) flags); \
107 void cleanup_field_fn(base_type *obj); \
108 field_type *get_field_fn(base_type *obj); \
109 field_type *make_field_fn(base_type *obj, typeof(GFP_KERNEL) flags); \
110 void cleanup_field_type_fn(void)
112 #define DECLARE_SHADOW_FIELD(base_type, field_type, name) \
113 __DECLARE_SHADOW_FIELD(base_type, field_type, init_##name##_shadows, \
114 init_##name##_shadow, get_##name##_shadow, \
115 make_##name##_shadow, cleanup_##name##_shadow, \
116 cleanup_##name##_shadows)
118 #endif /* __KERNEL__ */
120 #endif /* _KSPLICE_PATCH_H */