Skip to content

Commit eeebbde

Browse files
dwmw2Ingo Molnar
authored andcommitted
x86/kexec: Invoke copy of relocate_kernel() instead of the original
This currently calls set_memory_x() from machine_kexec_prepare() just like the 32-bit version does. That's actually a bit earlier than I'd like, as it leaves the page RWX all the time the image is even *loaded*. Subsequent commits will eliminate all the writes to the page between the point it's marked executable in machine_kexec_prepare() the time that relocate_kernel() is running and has switched to the identmap %cr3, so that it can be ROX. But that can't happen until it's moved to the .data section of the kernel, and *that* can't happen until we start executing the copy instead of executing it in place in the kernel .text. So break the circular dependency in those commits by letting it be RWX for now. Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Baoquan He <[email protected]> Cc: Vivek Goyal <[email protected]> Cc: Dave Young <[email protected]> Cc: Eric Biederman <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6a750b4 commit eeebbde

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

arch/x86/kernel/machine_kexec_64.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd,
157157
pmd_t *pmd;
158158
pte_t *pte;
159159

160-
vaddr = (unsigned long)relocate_kernel;
160+
/*
161+
* For the transition to the identity mapped page tables, the control
162+
* code page also needs to be mapped at the virtual address it starts
163+
* off running from.
164+
*/
165+
vaddr = (unsigned long)__va(control_page);
161166
paddr = control_page;
162167
pgd += pgd_index(vaddr);
163168
if (!pgd_present(*pgd)) {
@@ -311,11 +316,17 @@ int machine_kexec_prepare(struct kimage *image)
311316

312317
__memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
313318

319+
set_memory_x((unsigned long)control_page, 1);
320+
314321
return 0;
315322
}
316323

317324
void machine_kexec_cleanup(struct kimage *image)
318325
{
326+
void *control_page = page_address(image->control_code_page);
327+
328+
set_memory_nx((unsigned long)control_page, 1);
329+
319330
free_transition_pgtable(image);
320331
}
321332

@@ -325,6 +336,11 @@ void machine_kexec_cleanup(struct kimage *image)
325336
*/
326337
void machine_kexec(struct kimage *image)
327338
{
339+
unsigned long (*relocate_kernel_ptr)(unsigned long indirection_page,
340+
unsigned long page_list,
341+
unsigned long start_address,
342+
unsigned int preserve_context,
343+
unsigned int host_mem_enc_active);
328344
unsigned long page_list[PAGES_NR];
329345
unsigned int host_mem_enc_active;
330346
int save_ftrace_enabled;
@@ -371,6 +387,8 @@ void machine_kexec(struct kimage *image)
371387
page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
372388
<< PAGE_SHIFT);
373389

390+
relocate_kernel_ptr = control_page;
391+
374392
/*
375393
* The segment registers are funny things, they have both a
376394
* visible and an invisible part. Whenever the visible part is
@@ -390,11 +408,11 @@ void machine_kexec(struct kimage *image)
390408
native_gdt_invalidate();
391409

392410
/* now call it */
393-
image->start = relocate_kernel((unsigned long)image->head,
394-
(unsigned long)page_list,
395-
image->start,
396-
image->preserve_context,
397-
host_mem_enc_active);
411+
image->start = relocate_kernel_ptr((unsigned long)image->head,
412+
(unsigned long)page_list,
413+
image->start,
414+
image->preserve_context,
415+
host_mem_enc_active);
398416

399417
#ifdef CONFIG_KEXEC_JUMP
400418
if (image->preserve_context)

arch/x86/kernel/relocate_kernel_64.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define CP_PA_TABLE_PAGE DATA(0x20)
4040
#define CP_PA_SWAP_PAGE DATA(0x28)
4141
#define CP_PA_BACKUP_PAGES_MAP DATA(0x30)
42+
#define CP_VA_CONTROL_PAGE DATA(0x38)
4243

4344
.text
4445
.align PAGE_SIZE
@@ -99,6 +100,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel)
99100
movq %r9, CP_PA_TABLE_PAGE(%r11)
100101
movq %r10, CP_PA_SWAP_PAGE(%r11)
101102
movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11)
103+
movq %r11, CP_VA_CONTROL_PAGE(%r11)
102104

103105
/* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */
104106
movq %rcx, %r11
@@ -235,7 +237,8 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
235237
movq %rax, %cr3
236238
lea PAGE_SIZE(%r8), %rsp
237239
call swap_pages
238-
movq $virtual_mapped, %rax
240+
movq CP_VA_CONTROL_PAGE(%r8), %rax
241+
addq $(virtual_mapped - relocate_kernel), %rax
239242
pushq %rax
240243
ANNOTATE_UNRET_SAFE
241244
ret

0 commit comments

Comments
 (0)