diff options
author | Andi Kleen <ak@suse.de> | 2007-08-10 16:31:03 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-08-11 18:58:13 -0400 |
commit | ab144f5ec64c42218a555ec1dbde6b60cf2982d6 (patch) | |
tree | e3a4532e1db116e87060c9b18f4cfbf6258fdba3 /arch/i386/kernel/paravirt.c | |
parent | d3f3c9346979bfa074c64eac5fc3ed5bba4f40ed (diff) |
i386: Make patching more robust, fix paravirt issue
Commit 19d36ccdc34f5ed444f8a6af0cbfdb6790eb1177 "x86: Fix alternatives
and kprobes to remap write-protected kernel text" uses code which is
being patched for patching.
In particular, paravirt_ops does patching in two stages: first it
calls paravirt_ops.patch, then it fills any remaining instructions
with nop_out(). nop_out calls text_poke() which calls
lookup_address() which calls pgd_val() (aka paravirt_ops.pgd_val):
that call site is one of the places we patch.
If we always do patching as one single call to text_poke(), we only
need make sure we're not patching the memcpy in text_poke itself.
This means the prototype to paravirt_ops.patch needs to change, to
marshal the new code into a buffer rather than patching in place as it
does now. It also means all patching goes through text_poke(), which
is known to be safe (apply_alternatives is also changed to make a
single patch).
AK: fix compilation on x86-64 (bad rusty!)
AK: fix boot on x86-64 (sigh)
AK: merged with other patches
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'arch/i386/kernel/paravirt.c')
-rw-r--r-- | arch/i386/kernel/paravirt.c | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/arch/i386/kernel/paravirt.c b/arch/i386/kernel/paravirt.c index ea962c0667d5..739cfb207dd7 100644 --- a/arch/i386/kernel/paravirt.c +++ b/arch/i386/kernel/paravirt.c | |||
@@ -69,7 +69,8 @@ DEF_NATIVE(read_tsc, "rdtsc"); | |||
69 | 69 | ||
70 | DEF_NATIVE(ud2a, "ud2a"); | 70 | DEF_NATIVE(ud2a, "ud2a"); |
71 | 71 | ||
72 | static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len) | 72 | static unsigned native_patch(u8 type, u16 clobbers, void *ibuf, |
73 | unsigned long addr, unsigned len) | ||
73 | { | 74 | { |
74 | const unsigned char *start, *end; | 75 | const unsigned char *start, *end; |
75 | unsigned ret; | 76 | unsigned ret; |
@@ -90,7 +91,7 @@ static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len) | |||
90 | #undef SITE | 91 | #undef SITE |
91 | 92 | ||
92 | patch_site: | 93 | patch_site: |
93 | ret = paravirt_patch_insns(insns, len, start, end); | 94 | ret = paravirt_patch_insns(ibuf, len, start, end); |
94 | break; | 95 | break; |
95 | 96 | ||
96 | case PARAVIRT_PATCH(make_pgd): | 97 | case PARAVIRT_PATCH(make_pgd): |
@@ -107,7 +108,7 @@ static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len) | |||
107 | break; | 108 | break; |
108 | 109 | ||
109 | default: | 110 | default: |
110 | ret = paravirt_patch_default(type, clobbers, insns, len); | 111 | ret = paravirt_patch_default(type, clobbers, ibuf, addr, len); |
111 | break; | 112 | break; |
112 | } | 113 | } |
113 | 114 | ||
@@ -129,68 +130,67 @@ struct branch { | |||
129 | u32 delta; | 130 | u32 delta; |
130 | } __attribute__((packed)); | 131 | } __attribute__((packed)); |
131 | 132 | ||
132 | unsigned paravirt_patch_call(void *target, u16 tgt_clobbers, | 133 | unsigned paravirt_patch_call(void *insnbuf, |
133 | void *site, u16 site_clobbers, | 134 | const void *target, u16 tgt_clobbers, |
135 | unsigned long addr, u16 site_clobbers, | ||
134 | unsigned len) | 136 | unsigned len) |
135 | { | 137 | { |
136 | unsigned char *call = site; | 138 | struct branch *b = insnbuf; |
137 | unsigned long delta = (unsigned long)target - (unsigned long)(call+5); | 139 | unsigned long delta = (unsigned long)target - (addr+5); |
138 | struct branch b; | ||
139 | 140 | ||
140 | if (tgt_clobbers & ~site_clobbers) | 141 | if (tgt_clobbers & ~site_clobbers) |
141 | return len; /* target would clobber too much for this site */ | 142 | return len; /* target would clobber too much for this site */ |
142 | if (len < 5) | 143 | if (len < 5) |
143 | return len; /* call too long for patch site */ | 144 | return len; /* call too long for patch site */ |
144 | 145 | ||
145 | b.opcode = 0xe8; /* call */ | 146 | b->opcode = 0xe8; /* call */ |
146 | b.delta = delta; | 147 | b->delta = delta; |
147 | BUILD_BUG_ON(sizeof(b) != 5); | 148 | BUILD_BUG_ON(sizeof(*b) != 5); |
148 | text_poke(call, (unsigned char *)&b, 5); | ||
149 | 149 | ||
150 | return 5; | 150 | return 5; |
151 | } | 151 | } |
152 | 152 | ||
153 | unsigned paravirt_patch_jmp(void *target, void *site, unsigned len) | 153 | unsigned paravirt_patch_jmp(const void *target, void *insnbuf, |
154 | unsigned long addr, unsigned len) | ||
154 | { | 155 | { |
155 | unsigned char *jmp = site; | 156 | struct branch *b = insnbuf; |
156 | unsigned long delta = (unsigned long)target - (unsigned long)(jmp+5); | 157 | unsigned long delta = (unsigned long)target - (addr+5); |
157 | struct branch b; | ||
158 | 158 | ||
159 | if (len < 5) | 159 | if (len < 5) |
160 | return len; /* call too long for patch site */ | 160 | return len; /* call too long for patch site */ |
161 | 161 | ||
162 | b.opcode = 0xe9; /* jmp */ | 162 | b->opcode = 0xe9; /* jmp */ |
163 | b.delta = delta; | 163 | b->delta = delta; |
164 | text_poke(jmp, (unsigned char *)&b, 5); | ||
165 | 164 | ||
166 | return 5; | 165 | return 5; |
167 | } | 166 | } |
168 | 167 | ||
169 | unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len) | 168 | unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf, |
169 | unsigned long addr, unsigned len) | ||
170 | { | 170 | { |
171 | void *opfunc = *((void **)¶virt_ops + type); | 171 | void *opfunc = *((void **)¶virt_ops + type); |
172 | unsigned ret; | 172 | unsigned ret; |
173 | 173 | ||
174 | if (opfunc == NULL) | 174 | if (opfunc == NULL) |
175 | /* If there's no function, patch it with a ud2a (BUG) */ | 175 | /* If there's no function, patch it with a ud2a (BUG) */ |
176 | ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a); | 176 | ret = paravirt_patch_insns(insnbuf, len, start_ud2a, end_ud2a); |
177 | else if (opfunc == paravirt_nop) | 177 | else if (opfunc == paravirt_nop) |
178 | /* If the operation is a nop, then nop the callsite */ | 178 | /* If the operation is a nop, then nop the callsite */ |
179 | ret = paravirt_patch_nop(); | 179 | ret = paravirt_patch_nop(); |
180 | else if (type == PARAVIRT_PATCH(iret) || | 180 | else if (type == PARAVIRT_PATCH(iret) || |
181 | type == PARAVIRT_PATCH(irq_enable_sysexit)) | 181 | type == PARAVIRT_PATCH(irq_enable_sysexit)) |
182 | /* If operation requires a jmp, then jmp */ | 182 | /* If operation requires a jmp, then jmp */ |
183 | ret = paravirt_patch_jmp(opfunc, site, len); | 183 | ret = paravirt_patch_jmp(opfunc, insnbuf, addr, len); |
184 | else | 184 | else |
185 | /* Otherwise call the function; assume target could | 185 | /* Otherwise call the function; assume target could |
186 | clobber any caller-save reg */ | 186 | clobber any caller-save reg */ |
187 | ret = paravirt_patch_call(opfunc, CLBR_ANY, | 187 | ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY, |
188 | site, clobbers, len); | 188 | addr, clobbers, len); |
189 | 189 | ||
190 | return ret; | 190 | return ret; |
191 | } | 191 | } |
192 | 192 | ||
193 | unsigned paravirt_patch_insns(void *site, unsigned len, | 193 | unsigned paravirt_patch_insns(void *insnbuf, unsigned len, |
194 | const char *start, const char *end) | 194 | const char *start, const char *end) |
195 | { | 195 | { |
196 | unsigned insn_len = end - start; | 196 | unsigned insn_len = end - start; |
@@ -198,7 +198,7 @@ unsigned paravirt_patch_insns(void *site, unsigned len, | |||
198 | if (insn_len > len || start == NULL) | 198 | if (insn_len > len || start == NULL) |
199 | insn_len = len; | 199 | insn_len = len; |
200 | else | 200 | else |
201 | memcpy(site, start, insn_len); | 201 | memcpy(insnbuf, start, insn_len); |
202 | 202 | ||
203 | return insn_len; | 203 | return insn_len; |
204 | } | 204 | } |