Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Hibernation support for x86-64
0004  *
0005  * Copyright 2007 Rafael J. Wysocki <rjw@sisk.pl>
0006  * Copyright 2005 Andi Kleen <ak@suse.de>
0007  * Copyright 2004 Pavel Machek <pavel@suse.cz>
0008  *
0009  * swsusp_arch_resume must not use any stack or any nonlocal variables while
0010  * copying pages:
0011  *
0012  * Its rewriting one kernel image with another. What is stack in "old"
0013  * image could very well be data page in "new" image, and overwriting
0014  * your own stack under you is bad idea.
0015  */
0016 
0017     .text
0018 #include <linux/linkage.h>
0019 #include <asm/segment.h>
0020 #include <asm/page_types.h>
0021 #include <asm/asm-offsets.h>
0022 #include <asm/processor-flags.h>
0023 #include <asm/frame.h>
0024 #include <asm/nospec-branch.h>
0025 
0026      /* code below belongs to the image kernel */
0027     .align PAGE_SIZE
0028 SYM_FUNC_START(restore_registers)
0029     /* go back to the original page tables */
0030     movq    %r9, %cr3
0031 
0032     /* Flush TLB, including "global" things (vmalloc) */
0033     movq    mmu_cr4_features(%rip), %rax
0034     movq    %rax, %rdx
0035     andq    $~(X86_CR4_PGE), %rdx
0036     movq    %rdx, %cr4;  # turn off PGE
0037     movq    %cr3, %rcx;  # flush TLB
0038     movq    %rcx, %cr3
0039     movq    %rax, %cr4;  # turn PGE back on
0040 
0041     /* We don't restore %rax, it must be 0 anyway */
0042     movq    $saved_context, %rax
0043     movq    pt_regs_sp(%rax), %rsp
0044     movq    pt_regs_bp(%rax), %rbp
0045     movq    pt_regs_si(%rax), %rsi
0046     movq    pt_regs_di(%rax), %rdi
0047     movq    pt_regs_bx(%rax), %rbx
0048     movq    pt_regs_cx(%rax), %rcx
0049     movq    pt_regs_dx(%rax), %rdx
0050     movq    pt_regs_r8(%rax), %r8
0051     movq    pt_regs_r9(%rax), %r9
0052     movq    pt_regs_r10(%rax), %r10
0053     movq    pt_regs_r11(%rax), %r11
0054     movq    pt_regs_r12(%rax), %r12
0055     movq    pt_regs_r13(%rax), %r13
0056     movq    pt_regs_r14(%rax), %r14
0057     movq    pt_regs_r15(%rax), %r15
0058     pushq   pt_regs_flags(%rax)
0059     popfq
0060 
0061     /* Saved in save_processor_state. */
0062     lgdt    saved_context_gdt_desc(%rax)
0063 
0064     xorl    %eax, %eax
0065 
0066     /* tell the hibernation core that we've just restored the memory */
0067     movq    %rax, in_suspend(%rip)
0068 
0069     RET
0070 SYM_FUNC_END(restore_registers)
0071 
0072 SYM_FUNC_START(swsusp_arch_suspend)
0073     movq    $saved_context, %rax
0074     movq    %rsp, pt_regs_sp(%rax)
0075     movq    %rbp, pt_regs_bp(%rax)
0076     movq    %rsi, pt_regs_si(%rax)
0077     movq    %rdi, pt_regs_di(%rax)
0078     movq    %rbx, pt_regs_bx(%rax)
0079     movq    %rcx, pt_regs_cx(%rax)
0080     movq    %rdx, pt_regs_dx(%rax)
0081     movq    %r8, pt_regs_r8(%rax)
0082     movq    %r9, pt_regs_r9(%rax)
0083     movq    %r10, pt_regs_r10(%rax)
0084     movq    %r11, pt_regs_r11(%rax)
0085     movq    %r12, pt_regs_r12(%rax)
0086     movq    %r13, pt_regs_r13(%rax)
0087     movq    %r14, pt_regs_r14(%rax)
0088     movq    %r15, pt_regs_r15(%rax)
0089     pushfq
0090     popq    pt_regs_flags(%rax)
0091 
0092     /* save cr3 */
0093     movq    %cr3, %rax
0094     movq    %rax, restore_cr3(%rip)
0095 
0096     FRAME_BEGIN
0097     call swsusp_save
0098     FRAME_END
0099     RET
0100 SYM_FUNC_END(swsusp_arch_suspend)
0101 
0102 SYM_FUNC_START(restore_image)
0103     /* prepare to jump to the image kernel */
0104     movq    restore_jump_address(%rip), %r8
0105     movq    restore_cr3(%rip), %r9
0106 
0107     /* prepare to switch to temporary page tables */
0108     movq    temp_pgt(%rip), %rax
0109     movq    mmu_cr4_features(%rip), %rbx
0110 
0111     /* prepare to copy image data to their original locations */
0112     movq    restore_pblist(%rip), %rdx
0113 
0114     /* jump to relocated restore code */
0115     movq    relocated_restore_code(%rip), %rcx
0116     ANNOTATE_RETPOLINE_SAFE
0117     jmpq    *%rcx
0118 SYM_FUNC_END(restore_image)
0119 
0120     /* code below has been relocated to a safe page */
0121 SYM_FUNC_START(core_restore_code)
0122     /* switch to temporary page tables */
0123     movq    %rax, %cr3
0124     /* flush TLB */
0125     movq    %rbx, %rcx
0126     andq    $~(X86_CR4_PGE), %rcx
0127     movq    %rcx, %cr4;  # turn off PGE
0128     movq    %cr3, %rcx;  # flush TLB
0129     movq    %rcx, %cr3;
0130     movq    %rbx, %cr4;  # turn PGE back on
0131 .Lloop:
0132     testq   %rdx, %rdx
0133     jz  .Ldone
0134 
0135     /* get addresses from the pbe and copy the page */
0136     movq    pbe_address(%rdx), %rsi
0137     movq    pbe_orig_address(%rdx), %rdi
0138     movq    $(PAGE_SIZE >> 3), %rcx
0139     rep
0140     movsq
0141 
0142     /* progress to the next pbe */
0143     movq    pbe_next(%rdx), %rdx
0144     jmp .Lloop
0145 
0146 .Ldone:
0147     /* jump to the restore_registers address from the image header */
0148     ANNOTATE_RETPOLINE_SAFE
0149     jmpq    *%r8
0150 SYM_FUNC_END(core_restore_code)