blob: 3177c2bc26f63e9fe1bb82739be9c0c2e406f00f [file] [log] [blame]
Rafael J. Wysockicf7700f2008-02-09 23:24:09 +01001/*
2 * Hibernation support for x86-64
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Distribute under GPLv2.
5 *
Rafael J. Wysockicf7700f2008-02-09 23:24:09 +01006 * Copyright 2007 Rafael J. Wysocki <rjw@sisk.pl>
7 * Copyright 2005 Andi Kleen <ak@suse.de>
8 * Copyright 2004 Pavel Machek <pavel@suse.cz>
9 *
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070010 * swsusp_arch_resume must not use any stack or any nonlocal variables while
11 * copying pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Its rewriting one kernel image with another. What is stack in "old"
14 * image could very well be data page in "new" image, and overwriting
15 * your own stack under you is bad idea.
16 */
Rafael J. Wysockicf7700f2008-02-09 23:24:09 +010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 .text
19#include <linux/linkage.h>
20#include <asm/segment.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080021#include <asm/page_types.h>
Sam Ravnborge2d5df92005-09-09 21:28:48 +020022#include <asm/asm-offsets.h>
Cyrill Gorcunovbbb1e572008-02-09 00:49:13 +030023#include <asm/processor-flags.h>
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -060024#include <asm/frame.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26ENTRY(swsusp_arch_suspend)
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -060027 FRAME_BEGIN
Rafael J. Wysocki0de80bc2007-10-23 22:37:24 +020028 movq $saved_context, %rax
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010029 movq %rsp, pt_regs_sp(%rax)
30 movq %rbp, pt_regs_bp(%rax)
31 movq %rsi, pt_regs_si(%rax)
32 movq %rdi, pt_regs_di(%rax)
33 movq %rbx, pt_regs_bx(%rax)
34 movq %rcx, pt_regs_cx(%rax)
35 movq %rdx, pt_regs_dx(%rax)
Rafael J. Wysocki0de80bc2007-10-23 22:37:24 +020036 movq %r8, pt_regs_r8(%rax)
37 movq %r9, pt_regs_r9(%rax)
38 movq %r10, pt_regs_r10(%rax)
39 movq %r11, pt_regs_r11(%rax)
40 movq %r12, pt_regs_r12(%rax)
41 movq %r13, pt_regs_r13(%rax)
42 movq %r14, pt_regs_r14(%rax)
43 movq %r15, pt_regs_r15(%rax)
44 pushfq
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010045 popq pt_regs_flags(%rax)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Rafael J. Wysockic30bb682007-10-18 03:04:54 -070047 /* save cr3 */
48 movq %cr3, %rax
49 movq %rax, restore_cr3(%rip)
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 call swsusp_save
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -060052 FRAME_END
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 ret
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -060054ENDPROC(swsusp_arch_suspend)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +020056ENTRY(restore_image)
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070057 /* prepare to jump to the image kernel */
Rafael J. Wysocki65c05542016-06-30 18:11:41 +020058 movq restore_jump_address(%rip), %r8
59 movq restore_cr3(%rip), %r9
60
61 /* prepare to switch to temporary page tables */
62 movq temp_level4_pgt(%rip), %rax
63 movq mmu_cr4_features(%rip), %rbx
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070064
65 /* prepare to copy image data to their original locations */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070066 movq restore_pblist(%rip), %rdx
Rafael J. Wysocki65c05542016-06-30 18:11:41 +020067
68 /* jump to relocated restore code */
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070069 movq relocated_restore_code(%rip), %rcx
70 jmpq *%rcx
71
72 /* code below has been relocated to a safe page */
73ENTRY(core_restore_code)
Rafael J. Wysocki65c05542016-06-30 18:11:41 +020074 /* switch to temporary page tables */
75 movq $__PAGE_OFFSET, %rcx
76 subq %rcx, %rax
77 movq %rax, %cr3
78 /* flush TLB */
79 movq %rbx, %rcx
80 andq $~(X86_CR4_PGE), %rcx
81 movq %rcx, %cr4; # turn off PGE
82 movq %cr3, %rcx; # flush TLB
83 movq %rcx, %cr3;
84 movq %rbx, %cr4; # turn PGE back on
Borislav Petkovff22e202015-04-12 21:45:06 +020085.Lloop:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 testq %rdx, %rdx
Borislav Petkovff22e202015-04-12 21:45:06 +020087 jz .Ldone
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 /* get addresses from the pbe and copy the page */
90 movq pbe_address(%rdx), %rsi
91 movq pbe_orig_address(%rdx), %rdi
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -070092 movq $(PAGE_SIZE >> 3), %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 rep
94 movsq
95
96 /* progress to the next pbe */
97 movq pbe_next(%rdx), %rdx
Borislav Petkovff22e202015-04-12 21:45:06 +020098 jmp .Lloop
Rafael J. Wysocki65c05542016-06-30 18:11:41 +020099
Borislav Petkovff22e202015-04-12 21:45:06 +0200100.Ldone:
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700101 /* jump to the restore_registers address from the image header */
Rafael J. Wysocki65c05542016-06-30 18:11:41 +0200102 jmpq *%r8
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700103
Rafael J. Wysocki65c05542016-06-30 18:11:41 +0200104 /* code below belongs to the image kernel */
105 .align PAGE_SIZE
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700106ENTRY(restore_registers)
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -0600107 FRAME_BEGIN
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200108 /* go back to the original page tables */
Rafael J. Wysocki65c05542016-06-30 18:11:41 +0200109 movq %r9, %cr3
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* Flush TLB, including "global" things (vmalloc) */
112 movq mmu_cr4_features(%rip), %rax
113 movq %rax, %rdx
Cyrill Gorcunovbbb1e572008-02-09 00:49:13 +0300114 andq $~(X86_CR4_PGE), %rdx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 movq %rdx, %cr4; # turn off PGE
116 movq %cr3, %rcx; # flush TLB
117 movq %rcx, %cr3
118 movq %rax, %cr4; # turn PGE back on
119
Rafael J. Wysocki0de80bc2007-10-23 22:37:24 +0200120 /* We don't restore %rax, it must be 0 anyway */
121 movq $saved_context, %rax
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100122 movq pt_regs_sp(%rax), %rsp
123 movq pt_regs_bp(%rax), %rbp
124 movq pt_regs_si(%rax), %rsi
125 movq pt_regs_di(%rax), %rdi
126 movq pt_regs_bx(%rax), %rbx
127 movq pt_regs_cx(%rax), %rcx
128 movq pt_regs_dx(%rax), %rdx
Rafael J. Wysocki0de80bc2007-10-23 22:37:24 +0200129 movq pt_regs_r8(%rax), %r8
130 movq pt_regs_r9(%rax), %r9
131 movq pt_regs_r10(%rax), %r10
132 movq pt_regs_r11(%rax), %r11
133 movq pt_regs_r12(%rax), %r12
134 movq pt_regs_r13(%rax), %r13
135 movq pt_regs_r14(%rax), %r14
136 movq pt_regs_r15(%rax), %r15
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100137 pushq pt_regs_flags(%rax)
Rafael J. Wysocki0de80bc2007-10-23 22:37:24 +0200138 popfq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Konrad Rzeszutek Wilkcc456c42013-05-01 21:53:30 -0400140 /* Saved in save_processor_state. */
141 lgdt saved_context_gdt_desc(%rax)
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 xorq %rax, %rax
144
Rafael J. Wysockid158cbd2007-10-18 03:04:53 -0700145 /* tell the hibernation core that we've just restored the memory */
146 movq %rax, in_suspend(%rip)
147
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -0600148 FRAME_END
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret
Josh Poimboeufef0f3ed2016-01-21 16:49:24 -0600150ENDPROC(restore_registers)