wakeup.S 10.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
.text
#include <linux/linkage.h>
#include <asm/segment.h>
4
#include <asm/pgtable.h>
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <asm/page.h>
#include <asm/msr.h>

# Copyright 2003 Pavel Machek <pavel@suse.cz>, distribute under GPLv2
#
# wakeup_code runs in real mode, and at unknown address (determined at run-time).
# Therefore it must only use relative jumps/calls. 
#
# Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
#
# If physical address of wakeup_code is 0x12345, BIOS should call us with
# cs = 0x1234, eip = 0x05
#

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#define BEEP \
	inb	$97, %al; 	\
	outb	%al, $0x80; 	\
	movb	$3, %al; 	\
	outb	%al, $97; 	\
	outb	%al, $0x80; 	\
	movb	$-74, %al; 	\
	outb	%al, $67; 	\
	outb	%al, $0x80; 	\
	movb	$-119, %al; 	\
	outb	%al, $66; 	\
	outb	%al, $0x80; 	\
	movb	$15, %al; 	\
	outb	%al, $66;

L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

ALIGN
	.align	16
ENTRY(wakeup_start)
wakeup_code:
	wakeup_code_start = .
	.code16

# Running in *copy* of this code, somewhere in low 1MB.

	movb	$0xa1, %al	;  outb %al, $0x80
	cli
	cld
	# setup data segment
	movw	%cs, %ax
49
	movw	%ax, %ds		# Make ds:0 point to wakeup_start
L
Linus Torvalds 已提交
50
	movw	%ax, %ss
51 52

	# Data segment must be set up before we can see whether to beep.
53
	testl   $4, realmode_flags - wakeup_code
54 55 56 57
	jz      1f
	BEEP
1:

58 59
					# Private stack is needed for ASUS board
	mov	$(wakeup_stack - wakeup_code), %sp
L
Linus Torvalds 已提交
60

61
	pushl	$0			# Kill any dangerous flags
L
Linus Torvalds 已提交
62 63 64 65 66 67
	popfl

	movl	real_magic - wakeup_code, %eax
	cmpl	$0x12345678, %eax
	jne	bogus_real_magic

68 69 70 71 72
  	call	verify_cpu			# Verify the cpu supports long
						# mode
	testl	%eax, %eax
	jnz	no_longmode

73
	testl	$1, realmode_flags - wakeup_code
L
Linus Torvalds 已提交
74 75 76
	jz	1f
	lcall   $0xc000,$3
	movw	%cs, %ax
77
	movw	%ax, %ds		# Bios might have played with that
L
Linus Torvalds 已提交
78 79 80
	movw	%ax, %ss
1:

81
	testl	$2, realmode_flags - wakeup_code
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92
	jz	1f
	mov	video_mode - wakeup_code, %ax
	call	mode_seta
1:

 	movw	$0xb800, %ax
	movw	%ax,%fs
	movw	$0x0e00 + 'L', %fs:(0x10)

	movb	$0xa2, %al	;  outb %al, $0x80
	
93 94 95 96 97 98 99 100 101
	mov	%ds, %ax			# Find 32bit wakeup_code addr
	movzx   %ax, %esi			# (Convert %ds:gdt to a liner ptr)
	shll    $4, %esi
						# Fix up the vectors
	addl    %esi, wakeup_32_vector - wakeup_code
	addl    %esi, wakeup_long64_vector - wakeup_code
	addl    %esi, gdt_48a + 2 - wakeup_code # Fixup the gdt pointer

	lidtl	%ds:idt_48a - wakeup_code
102
	lgdtl	%ds:gdt_48a - wakeup_code	# load gdt with whatever is
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
						# appropriate

	movl	$1, %eax			# protected mode (PE) bit
	lmsw	%ax				# This is it!
	jmp	1f
1:

110 111 112 113
	ljmpl   *(wakeup_32_vector - wakeup_code)

	.balign 4
wakeup_32_vector:
114
	.long   wakeup_32 - wakeup_code
115
	.word   __KERNEL32_CS, 0
L
Linus Torvalds 已提交
116 117 118 119 120 121

	.code32
wakeup_32:
# Running in this code, but at low address; paging is not yet turned on.
	movb	$0xa5, %al	;  outb %al, $0x80

122 123
	movl	$__KERNEL_DS, %eax
	movl	%eax, %ds
L
Linus Torvalds 已提交
124

125 126 127
	movw	$0x0e00 + 'i', %ds:(0xb8012)
	movb	$0xa8, %al	;  outb %al, $0x80;

L
Linus Torvalds 已提交
128 129 130 131
	/*
	 * Prepare for entering 64bits mode
	 */

132
	/* Enable PAE */
L
Linus Torvalds 已提交
133 134 135 136 137
	xorl	%eax, %eax
	btsl	$5, %eax
	movl	%eax, %cr4

	/* Setup early boot stage 4 level pagetables */
138
	leal    (wakeup_level4_pgt - wakeup_code)(%esi), %eax
L
Linus Torvalds 已提交
139 140
	movl	%eax, %cr3

141 142 143 144 145
        /* Check if nx is implemented */
        movl    $0x80000001, %eax
        cpuid
        movl    %edx,%edi

L
Linus Torvalds 已提交
146
	/* Enable Long Mode */
147
	xorl    %eax, %eax
L
Linus Torvalds 已提交
148 149
	btsl	$_EFER_LME, %eax

150
	/* No Execute supported? */
L
Linus Torvalds 已提交
151 152 153 154 155
	btl	$20,%edi
	jnc     1f
	btsl	$_EFER_NX, %eax
				
	/* Make changes effective */
156 157
1:	movl    $MSR_EFER, %ecx
	xorl    %edx, %edx
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165
	wrmsr

	xorl	%eax, %eax
	btsl	$31, %eax			/* Enable paging and in turn activate Long Mode */
	btsl	$0, %eax			/* Enable protected mode */

	/* Make changes effective */
	movl	%eax, %cr0
166

L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	/* At this point:
		CR4.PAE must be 1
		CS.L must be 0
		CR3 must point to PML4
		Next instruction must be a branch
		This must be on identity-mapped page
	*/
	/*
	 * At this point we're in long mode but in 32bit compatibility mode
	 * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
	 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
	 */

	/* Finally jump in 64bit mode */
182
        ljmp    *(wakeup_long64_vector - wakeup_code)(%esi)
L
Linus Torvalds 已提交
183

184 185
	.balign 4
wakeup_long64_vector:
186
	.long   wakeup_long64 - wakeup_code
187
	.word   __KERNEL_CS, 0
L
Linus Torvalds 已提交
188 189 190

.code64

191 192 193
	/* Hooray, we are in Long 64-bit mode (but still running in
	 * low memory)
	 */
L
Linus Torvalds 已提交
194 195 196 197 198 199 200
wakeup_long64:
	/*
	 * We must switch to a new descriptor in kernel space for the GDT
	 * because soon the kernel won't have access anymore to the userspace
	 * addresses where we're currently running on. We have to do that here
	 * because in 32bit we couldn't load a 64bit linear address.
	 */
201
	lgdt	cpu_gdt_descr
L
Linus Torvalds 已提交
202

203 204 205
	movw	$0x0e00 + 'n', %ds:(0xb8014)
	movb	$0xa9, %al	;  outb %al, $0x80

206 207 208 209 210
	movq    saved_magic, %rax
	movq    $0x123456789abcdef0, %rdx
	cmpq    %rdx, %rax
	jne     bogus_64_magic

L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220
	movw	$0x0e00 + 'u', %ds:(0xb8016)
	
	nop
	nop
	movw	$__KERNEL_DS, %ax
	movw	%ax, %ss	
	movw	%ax, %ds
	movw	%ax, %es
	movw	%ax, %fs
	movw	%ax, %gs
221
	movq	saved_rsp, %rsp
L
Linus Torvalds 已提交
222 223

	movw	$0x0e00 + 'x', %ds:(0xb8018)
224 225 226 227
	movq	saved_rbx, %rbx
	movq	saved_rdi, %rdi
	movq	saved_rsi, %rsi
	movq	saved_rbp, %rbp
L
Linus Torvalds 已提交
228 229

	movw	$0x0e00 + '!', %ds:(0xb801a)
230
	movq	saved_rip, %rax
L
Linus Torvalds 已提交
231 232 233 234 235 236
	jmp	*%rax

.code32

	.align	64	
gdta:
237
	/* Its good to keep gdt in sync with one in trampoline.S */
L
Linus Torvalds 已提交
238
	.word	0, 0, 0, 0			# dummy
239 240 241 242
	/* ??? Why I need the accessed bit set in order for this to work? */
	.quad   0x00cf9b000000ffff              # __KERNEL32_CS
	.quad   0x00af9b000000ffff              # __KERNEL_CS
	.quad   0x00cf93000000ffff              # __KERNEL_DS
L
Linus Torvalds 已提交
243 244 245 246 247 248

idt_48a:
	.word	0				# idt limit = 0
	.word	0, 0				# idt base = 0L

gdt_48a:
249
	.word	0x800				# gdt limit=2048,
L
Linus Torvalds 已提交
250
						#  256 GDT entries
251
	.long   gdta - wakeup_code              # gdt base (relocated in later)
L
Linus Torvalds 已提交
252 253 254
	
real_magic:	.quad 0
video_mode:	.quad 0
255
realmode_flags:	.quad 0
L
Linus Torvalds 已提交
256

257
.code16
L
Linus Torvalds 已提交
258
bogus_real_magic:
259
	movb	$0xba,%al	;  outb %al,$0x80
L
Linus Torvalds 已提交
260 261
	jmp bogus_real_magic

262 263
.code64
bogus_64_magic:
L
Linus Torvalds 已提交
264
	movb	$0xb3,%al	;  outb %al,$0x80
265
	jmp bogus_64_magic
L
Linus Torvalds 已提交
266

267 268 269 270
.code16
no_longmode:
	movb    $0xbc,%al       ;  outb %al,$0x80
	jmp no_longmode
L
Linus Torvalds 已提交
271

272
#include "../verify_cpu.S"
L
Linus Torvalds 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	
/* This code uses an extended set of video mode numbers. These include:
 * Aliases for standard modes
 *	NORMAL_VGA (-1)
 *	EXTENDED_VGA (-2)
 *	ASK_VGA (-3)
 * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
 * of compatibility when extending the table. These are between 0x00 and 0xff.
 */
#define VIDEO_FIRST_MENU 0x0000

/* Standard BIOS video modes (BIOS number + 0x0100) */
#define VIDEO_FIRST_BIOS 0x0100

/* VESA BIOS video modes (VESA number + 0x0200) */
#define VIDEO_FIRST_VESA 0x0200

/* Video7 special modes (BIOS number + 0x0900) */
#define VIDEO_FIRST_V7 0x0900

# Setting of user mode (AX=mode ID) => CF=success
294
.code16
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
mode_seta:
	movw	%ax, %bx
#if 0
	cmpb	$0xff, %ah
	jz	setalias

	testb	$VIDEO_RECALC>>8, %ah
	jnz	_setrec

	cmpb	$VIDEO_FIRST_RESOLUTION>>8, %ah
	jnc	setres
	
	cmpb	$VIDEO_FIRST_SPECIAL>>8, %ah
	jz	setspc

	cmpb	$VIDEO_FIRST_V7>>8, %ah
	jz	setv7
#endif
	
	cmpb	$VIDEO_FIRST_VESA>>8, %ah
	jnc	check_vesaa
#if 0	
	orb	%ah, %ah
	jz	setmenu
#endif
	
	decb	%ah
#	jz	setbios				  Add bios modes later

setbada:	clc
	ret

check_vesaa:
	subb	$VIDEO_FIRST_VESA>>8, %bh
	orw	$0x4000, %bx			# Use linear frame buffer
	movw	$0x4f02, %ax			# VESA BIOS mode set call
	int	$0x10
	cmpw	$0x004f, %ax			# AL=4f if implemented
	jnz	_setbada				# AH=0 if OK

	stc
	ret

_setbada: jmp setbada

wakeup_stack_begin:	# Stack grows down

.org	0xff0
wakeup_stack:		# Just below end of page

345 346 347 348 349 350 351
.org   0x1000
ENTRY(wakeup_level4_pgt)
	.quad   level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
	.fill   510,8,0
	/* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
	.quad   level3_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE

L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363
ENTRY(wakeup_end)
	
##
# acpi_copy_wakeup_routine
#
# Copy the above routine to low memory.
#
# Parameters:
# %rdi:	place to copy wakeup routine to
#
# Returned address is location of code in low memory (past data and stack)
#
364
	.code64
L
Linus Torvalds 已提交
365 366 367 368 369 370
ENTRY(acpi_copy_wakeup_routine)
	pushq	%rax
	pushq	%rdx

	movl	saved_video_mode, %edx
	movl	%edx, video_mode - wakeup_start (,%rdi)
371 372
	movl	acpi_realmode_flags, %edx
	movl	%edx, realmode_flags - wakeup_start (,%rdi)
L
Linus Torvalds 已提交
373 374 375 376
	movq	$0x12345678, real_magic - wakeup_start (,%rdi)
	movq	$0x123456789abcdef0, %rdx
	movq	%rdx, saved_magic

377 378 379 380
	movq    saved_magic, %rax
	movq    $0x123456789abcdef0, %rdx
	cmpq    %rdx, %rax
	jne     bogus_64_magic
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	# restore the regs we used
	popq	%rdx
	popq	%rax
ENTRY(do_suspend_lowlevel_s4bios)
	ret

	.align 2
	.p2align 4,,15
.globl do_suspend_lowlevel
	.type	do_suspend_lowlevel,@function
do_suspend_lowlevel:
.LFB5:
	subq	$8, %rsp
	xorl	%eax, %eax
	call	save_processor_state

	movq %rsp, saved_context_esp(%rip)
	movq %rax, saved_context_eax(%rip)
	movq %rbx, saved_context_ebx(%rip)
	movq %rcx, saved_context_ecx(%rip)
	movq %rdx, saved_context_edx(%rip)
	movq %rbp, saved_context_ebp(%rip)
	movq %rsi, saved_context_esi(%rip)
	movq %rdi, saved_context_edi(%rip)
	movq %r8,  saved_context_r08(%rip)
	movq %r9,  saved_context_r09(%rip)
	movq %r10, saved_context_r10(%rip)
	movq %r11, saved_context_r11(%rip)
	movq %r12, saved_context_r12(%rip)
	movq %r13, saved_context_r13(%rip)
	movq %r14, saved_context_r14(%rip)
	movq %r15, saved_context_r15(%rip)
	pushfq ; popq saved_context_eflags(%rip)

416
	movq	$.L97, saved_rip(%rip)
L
Linus Torvalds 已提交
417

418 419 420 421 422
	movq %rsp,saved_rsp
	movq %rbp,saved_rbp
	movq %rbx,saved_rbx
	movq %rdi,saved_rdi
	movq %rsi,saved_rsi
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

	addq	$8, %rsp
	movl	$3, %edi
	xorl	%eax, %eax
	jmp	acpi_enter_sleep_state
.L97:
	.p2align 4,,7
.L99:
	.align 4
	movl	$24, %eax
	movw %ax, %ds
	movq	saved_context+58(%rip), %rax
	movq %rax, %cr4
	movq	saved_context+50(%rip), %rax
	movq %rax, %cr3
	movq	saved_context+42(%rip), %rax
	movq %rax, %cr2
	movq	saved_context+34(%rip), %rax
	movq %rax, %cr0
	pushq saved_context_eflags(%rip) ; popfq
	movq saved_context_esp(%rip), %rsp
	movq saved_context_ebp(%rip), %rbp
	movq saved_context_eax(%rip), %rax
	movq saved_context_ebx(%rip), %rbx
	movq saved_context_ecx(%rip), %rcx
	movq saved_context_edx(%rip), %rdx
	movq saved_context_esi(%rip), %rsi
	movq saved_context_edi(%rip), %rdi
	movq saved_context_r08(%rip), %r8
	movq saved_context_r09(%rip), %r9
	movq saved_context_r10(%rip), %r10
	movq saved_context_r11(%rip), %r11
	movq saved_context_r12(%rip), %r12
	movq saved_context_r13(%rip), %r13
	movq saved_context_r14(%rip), %r14
	movq saved_context_r15(%rip), %r15

	xorl	%eax, %eax
	addq	$8, %rsp
	jmp	restore_processor_state
.LFE5:
.Lfe5:
	.size	do_suspend_lowlevel,.Lfe5-do_suspend_lowlevel
	
.data
ALIGN
469 470 471 472
ENTRY(saved_rbp)	.quad	0
ENTRY(saved_rsi)	.quad	0
ENTRY(saved_rdi)	.quad	0
ENTRY(saved_rbx)	.quad	0
L
Linus Torvalds 已提交
473

474 475
ENTRY(saved_rip)	.quad	0
ENTRY(saved_rsp)	.quad	0
L
Linus Torvalds 已提交
476 477

ENTRY(saved_magic)	.quad	0