2 #include <linux/linkage.h>
3 #include <asm/segment.h>
4 #include <asm/pgtable.h>
7 #include <asm/asm-offsets.h>
9 # Copyright 2003 Pavel Machek <pavel@suse.cz>, distribute under GPLv2
11 # wakeup_code runs in real mode, and at unknown address (determined at run-time).
12 # Therefore it must only use relative jumps/calls.
14 # Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
16 # If physical address of wakeup_code is 0x12345, BIOS should call us with
17 # cs = 0x1234, eip = 0x05
43 # Running in *copy* of this code, somewhere in low 1MB.
49 movw %ax, %ds # Make ds:0 point to wakeup_start
52 # Data segment must be set up before we can see whether to beep.
53 testl $4, realmode_flags - wakeup_code
58 # Private stack is needed for ASUS board
59 mov $(wakeup_stack - wakeup_code), %sp
61 pushl $0 # Kill any dangerous flags
64 movl real_magic - wakeup_code, %eax
65 cmpl $0x12345678, %eax
68 testl $1, realmode_flags - wakeup_code
72 movw %ax, %ds # Bios might have played with that
76 testl $2, realmode_flags - wakeup_code
78 mov video_mode - wakeup_code, %ax
82 mov %ds, %ax # Find 32bit wakeup_code addr
83 movzx %ax, %esi # (Convert %ds:gdt to a liner ptr)
86 addl %esi, wakeup_32_vector - wakeup_code
87 addl %esi, wakeup_long64_vector - wakeup_code
88 addl %esi, gdt_48a + 2 - wakeup_code # Fixup the gdt pointer
90 lidtl %ds:idt_48a - wakeup_code
91 lgdtl %ds:gdt_48a - wakeup_code # load gdt with whatever is
94 movl $1, %eax # protected mode (PE) bit
95 lmsw %ax # This is it!
99 ljmpl *(wakeup_32_vector - wakeup_code)
103 .long wakeup_32 - wakeup_code
104 .word __KERNEL32_CS, 0
108 # Running in this code, but at low address; paging is not yet turned on.
110 movl $__KERNEL_DS, %eax
114 * Prepare for entering 64bits mode
122 /* Setup early boot stage 4 level pagetables */
123 leal (wakeup_level4_pgt - wakeup_code)(%esi), %eax
126 /* Check if nx is implemented */
127 movl $0x80000001, %eax
131 /* Enable Long Mode */
133 btsl $_EFER_LME, %eax
135 /* No Execute supported? */
140 /* Make changes effective */
141 1: movl $MSR_EFER, %ecx
146 btsl $31, %eax /* Enable paging and in turn activate Long Mode */
147 btsl $0, %eax /* Enable protected mode */
149 /* Make changes effective */
155 CR3 must point to PML4
156 Next instruction must be a branch
157 This must be on identity-mapped page
160 * At this point we're in long mode but in 32bit compatibility mode
161 * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
162 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
163 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
166 /* Finally jump in 64bit mode */
167 ljmp *(wakeup_long64_vector - wakeup_code)(%esi)
170 wakeup_long64_vector:
171 .long wakeup_long64 - wakeup_code
176 /* Hooray, we are in Long 64-bit mode (but still running in
181 * We must switch to a new descriptor in kernel space for the GDT
182 * because soon the kernel won't have access anymore to the userspace
183 * addresses where we're currently running on. We have to do that here
184 * because in 32bit we couldn't load a 64bit linear address.
188 movq saved_magic, %rax
189 movq $0x123456789abcdef0, %rdx
195 movw $__KERNEL_DS, %ax
215 /* Its good to keep gdt in sync with one in trampoline.S */
216 .word 0, 0, 0, 0 # dummy
217 /* ??? Why I need the accessed bit set in order for this to work? */
218 .quad 0x00cf9b000000ffff # __KERNEL32_CS
219 .quad 0x00af9b000000ffff # __KERNEL_CS
220 .quad 0x00cf93000000ffff # __KERNEL_DS
223 .word 0 # idt limit = 0
224 .word 0, 0 # idt base = 0L
227 .word 0x800 # gdt limit=2048,
229 .long gdta - wakeup_code # gdt base (relocated in later)
233 realmode_flags: .quad 0
243 /* This code uses an extended set of video mode numbers. These include:
244 * Aliases for standard modes
248 * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
249 * of compatibility when extending the table. These are between 0x00 and 0xff.
251 #define VIDEO_FIRST_MENU 0x0000
253 /* Standard BIOS video modes (BIOS number + 0x0100) */
254 #define VIDEO_FIRST_BIOS 0x0100
256 /* VESA BIOS video modes (VESA number + 0x0200) */
257 #define VIDEO_FIRST_VESA 0x0200
259 /* Video7 special modes (BIOS number + 0x0900) */
260 #define VIDEO_FIRST_V7 0x0900
262 # Setting of user mode (AX=mode ID) => CF=success
264 # For now, we only handle VESA modes (0x0200..0x03ff). To handle other
265 # modes, we should probably compile in the video code from the boot
270 subb $VIDEO_FIRST_VESA>>8, %bh
279 orw $0x4000, %bx # Use linear frame buffer
280 movw $0x4f02, %ax # VESA BIOS mode set call
282 cmpw $0x004f, %ax # AL=4f if implemented
283 jnz setbad # AH=0 if OK
288 wakeup_stack_begin: # Stack grows down
291 wakeup_stack: # Just below end of page
294 ENTRY(wakeup_level4_pgt)
295 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
297 /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
298 .quad level3_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
303 # acpi_copy_wakeup_routine
305 # Copy the above routine to low memory.
308 # %rdi: place to copy wakeup routine to
310 # Returned address is location of code in low memory (past data and stack)
313 ENTRY(acpi_copy_wakeup_routine)
317 movl saved_video_mode, %edx
318 movl %edx, video_mode - wakeup_start (,%rdi)
319 movl acpi_realmode_flags, %edx
320 movl %edx, realmode_flags - wakeup_start (,%rdi)
321 movq $0x12345678, real_magic - wakeup_start (,%rdi)
322 movq $0x123456789abcdef0, %rdx
323 movq %rdx, saved_magic
325 movq saved_magic, %rax
326 movq $0x123456789abcdef0, %rdx
330 # restore the regs we used
333 ENTRY(do_suspend_lowlevel_s4bios)
338 .globl do_suspend_lowlevel
339 .type do_suspend_lowlevel,@function
344 call save_processor_state
346 movq $saved_context, %rax
347 movq %rsp, pt_regs_sp(%rax)
348 movq %rbp, pt_regs_bp(%rax)
349 movq %rsi, pt_regs_si(%rax)
350 movq %rdi, pt_regs_di(%rax)
351 movq %rbx, pt_regs_bx(%rax)
352 movq %rcx, pt_regs_cx(%rax)
353 movq %rdx, pt_regs_dx(%rax)
354 movq %r8, pt_regs_r8(%rax)
355 movq %r9, pt_regs_r9(%rax)
356 movq %r10, pt_regs_r10(%rax)
357 movq %r11, pt_regs_r11(%rax)
358 movq %r12, pt_regs_r12(%rax)
359 movq %r13, pt_regs_r13(%rax)
360 movq %r14, pt_regs_r14(%rax)
361 movq %r15, pt_regs_r15(%rax)
363 popq pt_regs_flags(%rax)
365 movq $.L97, saved_rip(%rip)
376 jmp acpi_enter_sleep_state
384 /* We don't restore %rax, it must be 0 anyway */
385 movq $saved_context, %rax
386 movq saved_context_cr4(%rax), %rbx
388 movq saved_context_cr3(%rax), %rbx
390 movq saved_context_cr2(%rax), %rbx
392 movq saved_context_cr0(%rax), %rbx
394 pushq pt_regs_flags(%rax)
396 movq pt_regs_sp(%rax), %rsp
397 movq pt_regs_bp(%rax), %rbp
398 movq pt_regs_si(%rax), %rsi
399 movq pt_regs_di(%rax), %rdi
400 movq pt_regs_bx(%rax), %rbx
401 movq pt_regs_cx(%rax), %rcx
402 movq pt_regs_dx(%rax), %rdx
403 movq pt_regs_r8(%rax), %r8
404 movq pt_regs_r9(%rax), %r9
405 movq pt_regs_r10(%rax), %r10
406 movq pt_regs_r11(%rax), %r11
407 movq pt_regs_r12(%rax), %r12
408 movq pt_regs_r13(%rax), %r13
409 movq pt_regs_r14(%rax), %r14
410 movq pt_regs_r15(%rax), %r15
414 jmp restore_processor_state
417 .size do_suspend_lowlevel,.Lfe5-do_suspend_lowlevel
421 ENTRY(saved_rbp) .quad 0
422 ENTRY(saved_rsi) .quad 0
423 ENTRY(saved_rdi) .quad 0
424 ENTRY(saved_rbx) .quad 0
426 ENTRY(saved_rip) .quad 0
427 ENTRY(saved_rsp) .quad 0
429 ENTRY(saved_magic) .quad 0