xref: /linux/arch/x86/power/hibernate_asm_64.S (revision 3c9de67dd37029cca1d0f391ff565b3809b40a1f)
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Hibernation support for x86-64
4 *
5 * Copyright 2007 Rafael J. Wysocki <rjw@sisk.pl>
6 * Copyright 2005 Andi Kleen <ak@suse.de>
7 * Copyright 2004 Pavel Machek <pavel@suse.cz>
8 *
9 * swsusp_arch_resume must not use any stack or any nonlocal variables while
10 * copying pages:
11 *
12 * Its rewriting one kernel image with another. What is stack in "old"
13 * image could very well be data page in "new" image, and overwriting
14 * your own stack under you is bad idea.
15 */
16
17	.text
18#include <linux/linkage.h>
19#include <asm/segment.h>
20#include <asm/page_types.h>
21#include <asm/asm-offsets.h>
22#include <asm/processor-flags.h>
23#include <asm/frame.h>
24#include <asm/nospec-branch.h>
25
26	 /* code below belongs to the image kernel */
27	.align PAGE_SIZE
28SYM_FUNC_START(restore_registers)
29	ENDBR
30	/* go back to the original page tables */
31	movq    %r9, %cr3
32
33	/* Flush TLB, including "global" things (vmalloc) */
34	movq	mmu_cr4_features(%rip), %rax
35	movq	%rax, %rdx
36	andq	$~(X86_CR4_PGE), %rdx
37	movq	%rdx, %cr4;  # turn off PGE
38	movq	%cr3, %rcx;  # flush TLB
39	movq	%rcx, %cr3
40	movq	%rax, %cr4;  # turn PGE back on
41
42	/* We don't restore %rax, it must be 0 anyway */
43	movq	$saved_context, %rax
44	movq	pt_regs_sp(%rax), %rsp
45	movq	pt_regs_bp(%rax), %rbp
46	movq	pt_regs_si(%rax), %rsi
47	movq	pt_regs_di(%rax), %rdi
48	movq	pt_regs_bx(%rax), %rbx
49	movq	pt_regs_cx(%rax), %rcx
50	movq	pt_regs_dx(%rax), %rdx
51	movq	pt_regs_r8(%rax), %r8
52	movq	pt_regs_r9(%rax), %r9
53	movq	pt_regs_r10(%rax), %r10
54	movq	pt_regs_r11(%rax), %r11
55	movq	pt_regs_r12(%rax), %r12
56	movq	pt_regs_r13(%rax), %r13
57	movq	pt_regs_r14(%rax), %r14
58	movq	pt_regs_r15(%rax), %r15
59	pushq	pt_regs_flags(%rax)
60	popfq
61
62	/* Saved in save_processor_state. */
63	lgdt	saved_context_gdt_desc(%rax)
64
65	xorl	%eax, %eax
66
67	/* tell the hibernation core that we've just restored the memory */
68	movq	%rax, in_suspend(%rip)
69
70	RET
71SYM_FUNC_END(restore_registers)
72
73SYM_FUNC_START(swsusp_arch_suspend)
74	movq	$saved_context, %rax
75	movq	%rsp, pt_regs_sp(%rax)
76	movq	%rbp, pt_regs_bp(%rax)
77	movq	%rsi, pt_regs_si(%rax)
78	movq	%rdi, pt_regs_di(%rax)
79	movq	%rbx, pt_regs_bx(%rax)
80	movq	%rcx, pt_regs_cx(%rax)
81	movq	%rdx, pt_regs_dx(%rax)
82	movq	%r8, pt_regs_r8(%rax)
83	movq	%r9, pt_regs_r9(%rax)
84	movq	%r10, pt_regs_r10(%rax)
85	movq	%r11, pt_regs_r11(%rax)
86	movq	%r12, pt_regs_r12(%rax)
87	movq	%r13, pt_regs_r13(%rax)
88	movq	%r14, pt_regs_r14(%rax)
89	movq	%r15, pt_regs_r15(%rax)
90	pushfq
91	popq	pt_regs_flags(%rax)
92
93	/* save cr3 */
94	movq	%cr3, %rax
95	movq	%rax, restore_cr3(%rip)
96
97	FRAME_BEGIN
98	call swsusp_save
99	FRAME_END
100	RET
101SYM_FUNC_END(swsusp_arch_suspend)
102
103SYM_FUNC_START(restore_image)
104	/* prepare to jump to the image kernel */
105	movq	restore_jump_address(%rip), %r8
106	movq	restore_cr3(%rip), %r9
107
108	/* prepare to switch to temporary page tables */
109	movq	temp_pgt(%rip), %rax
110	movq	mmu_cr4_features(%rip), %rbx
111
112	/* prepare to copy image data to their original locations */
113	movq	restore_pblist(%rip), %rdx
114
115	/* jump to relocated restore code */
116	movq	relocated_restore_code(%rip), %rcx
117	ANNOTATE_RETPOLINE_SAFE
118	jmpq	*%rcx
119SYM_FUNC_END(restore_image)
120
121	/* code below has been relocated to a safe page */
122SYM_FUNC_START(core_restore_code)
123	ENDBR
124	/* switch to temporary page tables */
125	movq	%rax, %cr3
126	/* flush TLB */
127	movq	%rbx, %rcx
128	andq	$~(X86_CR4_PGE), %rcx
129	movq	%rcx, %cr4;  # turn off PGE
130	movq	%cr3, %rcx;  # flush TLB
131	movq	%rcx, %cr3;
132	movq	%rbx, %cr4;  # turn PGE back on
133.Lloop:
134	testq	%rdx, %rdx
135	jz	.Ldone
136
137	/* get addresses from the pbe and copy the page */
138	movq	pbe_address(%rdx), %rsi
139	movq	pbe_orig_address(%rdx), %rdi
140	movq	$(PAGE_SIZE >> 3), %rcx
141	rep
142	movsq
143
144	/* progress to the next pbe */
145	movq	pbe_next(%rdx), %rdx
146	jmp	.Lloop
147
148.Ldone:
149	/* jump to the restore_registers address from the image header */
150	ANNOTATE_RETPOLINE_SAFE
151	jmpq	*%r8
152SYM_FUNC_END(core_restore_code)
153