2 #include <linux/linkage.h>
3 #include <asm/segment.h>
7 # Copyright 2003 Pavel Machek <pavel@suse.cz>, distribute under GPLv2
9 # wakeup_code runs in real mode, and at unknown address (determined at run-time).
10 # Therefore it must only use relative jumps/calls.
12 # Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
14 # If physical address of wakeup_code is 0x12345, BIOS should call us with
15 # cs = 0x1234, eip = 0x05
26 # Running in *copy* of this code, somewhere in low 1MB.
28 movb $0xa1, %al ; outb %al, $0x80
33 movw %ax, %ds # Make ds:0 point to wakeup_start
35 mov $(wakeup_stack - wakeup_code), %sp # Private stack is needed for ASUS board
37 pushl $0 # Kill any dangerous flags
40 movl real_magic - wakeup_code, %eax
41 cmpl $0x12345678, %eax
44 testl $1, video_flags - wakeup_code
48 movw %ax, %ds # Bios might have played with that
52 testl $2, video_flags - wakeup_code
54 mov video_mode - wakeup_code, %ax
60 movw $0x0e00 + 'L', %fs:(0x10)
62 movb $0xa2, %al ; outb %al, $0x80
64 lidt %ds:idt_48a - wakeup_code
66 movw %ds, %ax # (Convert %ds:gdt to a linear ptr)
68 addl $(gdta - wakeup_code), %eax
69 movl %eax, gdt_48a +2 - wakeup_code
70 lgdtl %ds:gdt_48a - wakeup_code # load gdt with whatever is
73 movl $1, %eax # protected mode (PE) bit
74 lmsw %ax # This is it!
78 .byte 0x66, 0xea # prefix + jmpi-opcode
79 .long wakeup_32 - __START_KERNEL_map
84 # Running in this code, but at low address; paging is not yet turned on.
85 movb $0xa5, %al ; outb %al, $0x80
87 /* Check if extended functions are implemented */
88 movl $0x80000000, %eax
90 cmpl $0x80000000, %eax
99 movw $__KERNEL_DS, %ax
105 movw $__KERNEL_DS, %ax
108 mov $(wakeup_stack - __START_KERNEL_map), %esp
109 movl saved_magic - __START_KERNEL_map, %eax
110 cmpl $0x9abcdef0, %eax
114 * Prepare for entering 64bits mode
117 /* Enable PAE mode and PGE */
123 /* Setup early boot stage 4 level pagetables */
124 movl $(wakeup_level4_pgt - __START_KERNEL_map), %eax
127 /* Setup EFER (Extended Feature Enable Register) */
130 /* Fool rdmsr and reset %eax to avoid dependences */
132 /* Enable Long Mode */
133 btsl $_EFER_LME, %eax
134 /* Enable System Call */
135 btsl $_EFER_SCE, %eax
137 /* No Execute supported? */
143 /* Make changes effective */
148 btsl $31, %eax /* Enable paging and in turn activate Long Mode */
149 btsl $0, %eax /* Enable protected mode */
150 btsl $1, %eax /* Enable MP */
151 btsl $4, %eax /* Enable ET */
152 btsl $5, %eax /* Enable NE */
153 btsl $16, %eax /* Enable WP */
154 btsl $18, %eax /* Enable AM */
156 /* Make changes effective */
161 CR3 must point to PML4
162 Next instruction must be a branch
163 This must be on identity-mapped page
165 jmp reach_compatibility_mode
166 reach_compatibility_mode:
167 movw $0x0e00 + 'i', %ds:(0xb8012)
168 movb $0xa8, %al ; outb %al, $0x80;
171 * At this point we're in long mode but in 32bit compatibility mode
172 * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
173 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
174 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
177 movw $0x0e00 + 'n', %ds:(0xb8014)
178 movb $0xa9, %al ; outb %al, $0x80
180 /* Load new GDT with the 64bit segment using 32bit descriptor */
181 movl $(pGDT32 - __START_KERNEL_map), %eax
184 movl $(wakeup_jumpvector - __START_KERNEL_map), %eax
185 /* Finally jump in 64bit mode */
189 .long wakeup_long64 - __START_KERNEL_map
194 /* Hooray, we are in Long 64-bit mode (but still running in low memory) */
197 * We must switch to a new descriptor in kernel space for the GDT
198 * because soon the kernel won't have access anymore to the userspace
199 * addresses where we're currently running on. We have to do that here
200 * because in 32bit we couldn't load a 64bit linear address.
202 lgdt cpu_gdt_descr - __START_KERNEL_map
204 movw $0x0e00 + 'u', %ds:(0xb8016)
208 movw $__KERNEL_DS, %ax
216 movw $0x0e00 + 'x', %ds:(0xb8018)
222 movw $0x0e00 + '!', %ds:(0xb801a)
230 .word 0, 0, 0, 0 # dummy
232 .word 0, 0, 0, 0 # unused
234 .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
235 .word 0 # base address = 0
236 .word 0x9B00 # code read/exec. ??? Why I need 0x9B00 (as opposed to 0x9A00 in order for this to work?)
237 .word 0x00CF # granularity = 4096, 386
238 # (+5th nibble of limit)
240 .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
241 .word 0 # base address = 0
242 .word 0x9200 # data read/write
243 .word 0x00CF # granularity = 4096, 386
244 # (+5th nibble of limit)
245 # this is 64bit descriptor for code
248 .word 0x9A00 # code read/exec
249 .word 0x00AF # as above, but it is long mode and with D=0
252 .word 0 # idt limit = 0
253 .word 0, 0 # idt base = 0L
256 .word 0x8000 # gdt limit=2048,
258 .word 0, 0 # gdt base (filled in later)
261 real_save_gdt: .word 0
268 movb $0xba,%al ; outb %al,$0x80
272 movb $0xb3,%al ; outb %al,$0x80
276 movb $0xb1,%al ; outb %al,$0x80
280 movb $0xbc,%al ; outb %al,$0x80
284 /* This code uses an extended set of video mode numbers. These include:
285 * Aliases for standard modes
289 * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
290 * of compatibility when extending the table. These are between 0x00 and 0xff.
292 #define VIDEO_FIRST_MENU 0x0000
294 /* Standard BIOS video modes (BIOS number + 0x0100) */
295 #define VIDEO_FIRST_BIOS 0x0100
297 /* VESA BIOS video modes (VESA number + 0x0200) */
298 #define VIDEO_FIRST_VESA 0x0200
300 /* Video7 special modes (BIOS number + 0x0900) */
301 #define VIDEO_FIRST_V7 0x0900
303 # Setting of user mode (AX=mode ID) => CF=success
310 testb $VIDEO_RECALC>>8, %ah
313 cmpb $VIDEO_FIRST_RESOLUTION>>8, %ah
316 cmpb $VIDEO_FIRST_SPECIAL>>8, %ah
319 cmpb $VIDEO_FIRST_V7>>8, %ah
323 cmpb $VIDEO_FIRST_VESA>>8, %ah
331 # jz setbios Add bios modes later
337 subb $VIDEO_FIRST_VESA>>8, %bh
338 orw $0x4000, %bx # Use linear frame buffer
339 movw $0x4f02, %ax # VESA BIOS mode set call
341 cmpw $0x004f, %ax # AL=4f if implemented
342 jnz _setbada # AH=0 if OK
347 _setbada: jmp setbada
351 movw $0x0e00 + 'B', %ds:(0xb8018)
355 movw $0x0e00 + '2', %ds:(0xb8018)
359 wakeup_stack_begin: # Stack grows down
362 wakeup_stack: # Just below end of page
367 # acpi_copy_wakeup_routine
369 # Copy the above routine to low memory.
372 # %rdi: place to copy wakeup routine to
374 # Returned address is location of code in low memory (past data and stack)
376 ENTRY(acpi_copy_wakeup_routine)
392 sgdt real_save_gdt - wakeup_start (,%rdi)
395 movl %eax, saved_efer
396 movl %edx, saved_efer2
398 movl saved_video_mode, %edx
399 movl %edx, video_mode - wakeup_start (,%rdi)
400 movl acpi_video_flags, %edx
401 movl %edx, video_flags - wakeup_start (,%rdi)
402 movq $0x12345678, real_magic - wakeup_start (,%rdi)
403 movq $0x123456789abcdef0, %rdx
404 movq %rdx, saved_magic
406 movl saved_magic - __START_KERNEL_map, %eax
407 cmpl $0x9abcdef0, %eax
410 # make sure %cr4 is set correctly (features, etc)
411 movl saved_cr4 - __START_KERNEL_map, %eax
414 movl saved_cr0 - __START_KERNEL_map, %eax
416 jmp 1f # Flush pipelines
418 # restore the regs we used
422 ENTRY(do_suspend_lowlevel_s4bios)
427 .globl do_suspend_lowlevel
428 .type do_suspend_lowlevel,@function
433 call save_processor_state
435 movq %rsp, saved_context_esp(%rip)
436 movq %rax, saved_context_eax(%rip)
437 movq %rbx, saved_context_ebx(%rip)
438 movq %rcx, saved_context_ecx(%rip)
439 movq %rdx, saved_context_edx(%rip)
440 movq %rbp, saved_context_ebp(%rip)
441 movq %rsi, saved_context_esi(%rip)
442 movq %rdi, saved_context_edi(%rip)
443 movq %r8, saved_context_r08(%rip)
444 movq %r9, saved_context_r09(%rip)
445 movq %r10, saved_context_r10(%rip)
446 movq %r11, saved_context_r11(%rip)
447 movq %r12, saved_context_r12(%rip)
448 movq %r13, saved_context_r13(%rip)
449 movq %r14, saved_context_r14(%rip)
450 movq %r15, saved_context_r15(%rip)
451 pushfq ; popq saved_context_eflags(%rip)
453 movq $.L97, saved_eip(%rip)
464 jmp acpi_enter_sleep_state
471 movq saved_context+58(%rip), %rax
473 movq saved_context+50(%rip), %rax
475 movq saved_context+42(%rip), %rax
477 movq saved_context+34(%rip), %rax
479 pushq saved_context_eflags(%rip) ; popfq
480 movq saved_context_esp(%rip), %rsp
481 movq saved_context_ebp(%rip), %rbp
482 movq saved_context_eax(%rip), %rax
483 movq saved_context_ebx(%rip), %rbx
484 movq saved_context_ecx(%rip), %rcx
485 movq saved_context_edx(%rip), %rdx
486 movq saved_context_esi(%rip), %rsi
487 movq saved_context_edi(%rip), %rdi
488 movq saved_context_r08(%rip), %r8
489 movq saved_context_r09(%rip), %r9
490 movq saved_context_r10(%rip), %r10
491 movq saved_context_r11(%rip), %r11
492 movq saved_context_r12(%rip), %r12
493 movq saved_context_r13(%rip), %r13
494 movq saved_context_r14(%rip), %r14
495 movq saved_context_r15(%rip), %r15
499 jmp restore_processor_state
502 .size do_suspend_lowlevel,.Lfe5-do_suspend_lowlevel
506 ENTRY(saved_ebp) .quad 0
507 ENTRY(saved_esi) .quad 0
508 ENTRY(saved_edi) .quad 0
509 ENTRY(saved_ebx) .quad 0
511 ENTRY(saved_eip) .quad 0
512 ENTRY(saved_esp) .quad 0
514 ENTRY(saved_magic) .quad 0