wakeup_64.S 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. .text
  2. #include <linux/linkage.h>
  3. #include <asm/segment.h>
  4. #include <asm/pgtable.h>
  5. #include <asm/page.h>
  6. #include <asm/msr.h>
  7. # Copyright 2003 Pavel Machek <pavel@suse.cz>, distribute under GPLv2
  8. #
  9. # wakeup_code runs in real mode, and at unknown address (determined at run-time).
  10. # Therefore it must only use relative jumps/calls.
  11. #
  12. # Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
  13. #
  14. # If physical address of wakeup_code is 0x12345, BIOS should call us with
  15. # cs = 0x1234, eip = 0x05
  16. #
  17. #define BEEP \
  18. inb $97, %al; \
  19. outb %al, $0x80; \
  20. movb $3, %al; \
  21. outb %al, $97; \
  22. outb %al, $0x80; \
  23. movb $-74, %al; \
  24. outb %al, $67; \
  25. outb %al, $0x80; \
  26. movb $-119, %al; \
  27. outb %al, $66; \
  28. outb %al, $0x80; \
  29. movb $15, %al; \
  30. outb %al, $66;
  31. ALIGN
  32. .align 16
  33. ENTRY(wakeup_start)
  34. wakeup_code:
  35. wakeup_code_start = .
  36. .code16
  37. # Running in *copy* of this code, somewhere in low 1MB.
  38. cli
  39. cld
  40. # setup data segment
  41. movw %cs, %ax
  42. movw %ax, %ds # Make ds:0 point to wakeup_start
  43. movw %ax, %ss
  44. # Data segment must be set up before we can see whether to beep.
  45. testl $4, realmode_flags - wakeup_code
  46. jz 1f
  47. BEEP
  48. 1:
  49. # Private stack is needed for ASUS board
  50. mov $(wakeup_stack - wakeup_code), %sp
  51. pushl $0 # Kill any dangerous flags
  52. popfl
  53. movl real_magic - wakeup_code, %eax
  54. cmpl $0x12345678, %eax
  55. jne bogus_real_magic
  56. testl $1, realmode_flags - wakeup_code
  57. jz 1f
  58. lcall $0xc000,$3
  59. movw %cs, %ax
  60. movw %ax, %ds # Bios might have played with that
  61. movw %ax, %ss
  62. 1:
  63. testl $2, realmode_flags - wakeup_code
  64. jz 1f
  65. mov video_mode - wakeup_code, %ax
  66. call mode_set
  67. 1:
  68. mov %ds, %ax # Find 32bit wakeup_code addr
  69. movzx %ax, %esi # (Convert %ds:gdt to a liner ptr)
  70. shll $4, %esi
  71. # Fix up the vectors
  72. addl %esi, wakeup_32_vector - wakeup_code
  73. addl %esi, wakeup_long64_vector - wakeup_code
  74. addl %esi, gdt_48a + 2 - wakeup_code # Fixup the gdt pointer
  75. lidtl %ds:idt_48a - wakeup_code
  76. lgdtl %ds:gdt_48a - wakeup_code # load gdt with whatever is
  77. # appropriate
  78. movl $1, %eax # protected mode (PE) bit
  79. lmsw %ax # This is it!
  80. jmp 1f
  81. 1:
  82. ljmpl *(wakeup_32_vector - wakeup_code)
  83. .balign 4
  84. wakeup_32_vector:
  85. .long wakeup_32 - wakeup_code
  86. .word __KERNEL32_CS, 0
  87. .code32
  88. wakeup_32:
  89. # Running in this code, but at low address; paging is not yet turned on.
  90. movl $__KERNEL_DS, %eax
  91. movl %eax, %ds
  92. /*
  93. * Prepare for entering 64bits mode
  94. */
  95. /* Enable PAE */
  96. xorl %eax, %eax
  97. btsl $5, %eax
  98. movl %eax, %cr4
  99. /* Setup early boot stage 4 level pagetables */
  100. leal (wakeup_level4_pgt - wakeup_code)(%esi), %eax
  101. movl %eax, %cr3
  102. /* Check if nx is implemented */
  103. movl $0x80000001, %eax
  104. cpuid
  105. movl %edx,%edi
  106. /* Enable Long Mode */
  107. xorl %eax, %eax
  108. btsl $_EFER_LME, %eax
  109. /* No Execute supported? */
  110. btl $20,%edi
  111. jnc 1f
  112. btsl $_EFER_NX, %eax
  113. /* Make changes effective */
  114. 1: movl $MSR_EFER, %ecx
  115. xorl %edx, %edx
  116. wrmsr
  117. xorl %eax, %eax
  118. btsl $31, %eax /* Enable paging and in turn activate Long Mode */
  119. btsl $0, %eax /* Enable protected mode */
  120. /* Make changes effective */
  121. movl %eax, %cr0
  122. /* At this point:
  123. CR4.PAE must be 1
  124. CS.L must be 0
  125. CR3 must point to PML4
  126. Next instruction must be a branch
  127. This must be on identity-mapped page
  128. */
  129. /*
  130. * At this point we're in long mode but in 32bit compatibility mode
  131. * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
  132. * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
  133. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  134. */
  135. /* Finally jump in 64bit mode */
  136. ljmp *(wakeup_long64_vector - wakeup_code)(%esi)
  137. .balign 4
  138. wakeup_long64_vector:
  139. .long wakeup_long64 - wakeup_code
  140. .word __KERNEL_CS, 0
  141. .code64
  142. /* Hooray, we are in Long 64-bit mode (but still running in
  143. * low memory)
  144. */
  145. wakeup_long64:
  146. /*
  147. * We must switch to a new descriptor in kernel space for the GDT
  148. * because soon the kernel won't have access anymore to the userspace
  149. * addresses where we're currently running on. We have to do that here
  150. * because in 32bit we couldn't load a 64bit linear address.
  151. */
  152. lgdt cpu_gdt_descr
  153. movq saved_magic, %rax
  154. movq $0x123456789abcdef0, %rdx
  155. cmpq %rdx, %rax
  156. jne bogus_64_magic
  157. nop
  158. nop
  159. movw $__KERNEL_DS, %ax
  160. movw %ax, %ss
  161. movw %ax, %ds
  162. movw %ax, %es
  163. movw %ax, %fs
  164. movw %ax, %gs
  165. movq saved_rsp, %rsp
  166. movq saved_rbx, %rbx
  167. movq saved_rdi, %rdi
  168. movq saved_rsi, %rsi
  169. movq saved_rbp, %rbp
  170. movq saved_rip, %rax
  171. jmp *%rax
  172. .code32
  173. .align 64
  174. gdta:
  175. /* Its good to keep gdt in sync with one in trampoline.S */
  176. .word 0, 0, 0, 0 # dummy
  177. /* ??? Why I need the accessed bit set in order for this to work? */
  178. .quad 0x00cf9b000000ffff # __KERNEL32_CS
  179. .quad 0x00af9b000000ffff # __KERNEL_CS
  180. .quad 0x00cf93000000ffff # __KERNEL_DS
  181. idt_48a:
  182. .word 0 # idt limit = 0
  183. .word 0, 0 # idt base = 0L
  184. gdt_48a:
  185. .word 0x800 # gdt limit=2048,
  186. # 256 GDT entries
  187. .long gdta - wakeup_code # gdt base (relocated in later)
  188. real_magic: .quad 0
  189. video_mode: .quad 0
  190. realmode_flags: .quad 0
  191. .code16
  192. bogus_real_magic:
  193. jmp bogus_real_magic
  194. .code64
  195. bogus_64_magic:
  196. jmp bogus_64_magic
  197. /* This code uses an extended set of video mode numbers. These include:
  198. * Aliases for standard modes
  199. * NORMAL_VGA (-1)
  200. * EXTENDED_VGA (-2)
  201. * ASK_VGA (-3)
  202. * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
  203. * of compatibility when extending the table. These are between 0x00 and 0xff.
  204. */
  205. #define VIDEO_FIRST_MENU 0x0000
  206. /* Standard BIOS video modes (BIOS number + 0x0100) */
  207. #define VIDEO_FIRST_BIOS 0x0100
  208. /* VESA BIOS video modes (VESA number + 0x0200) */
  209. #define VIDEO_FIRST_VESA 0x0200
  210. /* Video7 special modes (BIOS number + 0x0900) */
  211. #define VIDEO_FIRST_V7 0x0900
  212. # Setting of user mode (AX=mode ID) => CF=success
  213. # For now, we only handle VESA modes (0x0200..0x03ff). To handle other
  214. # modes, we should probably compile in the video code from the boot
  215. # directory.
  216. .code16
  217. mode_set:
  218. movw %ax, %bx
  219. subb $VIDEO_FIRST_VESA>>8, %bh
  220. cmpb $2, %bh
  221. jb check_vesa
  222. setbad:
  223. clc
  224. ret
  225. check_vesa:
  226. orw $0x4000, %bx # Use linear frame buffer
  227. movw $0x4f02, %ax # VESA BIOS mode set call
  228. int $0x10
  229. cmpw $0x004f, %ax # AL=4f if implemented
  230. jnz setbad # AH=0 if OK
  231. stc
  232. ret
  233. wakeup_stack_begin: # Stack grows down
  234. .org 0xff0
  235. wakeup_stack: # Just below end of page
  236. .org 0x1000
  237. ENTRY(wakeup_level4_pgt)
  238. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  239. .fill 510,8,0
  240. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  241. .quad level3_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  242. ENTRY(wakeup_end)
  243. ##
  244. # acpi_copy_wakeup_routine
  245. #
  246. # Copy the above routine to low memory.
  247. #
  248. # Parameters:
  249. # %rdi: place to copy wakeup routine to
  250. #
  251. # Returned address is location of code in low memory (past data and stack)
  252. #
  253. .code64
  254. ENTRY(acpi_copy_wakeup_routine)
  255. pushq %rax
  256. pushq %rdx
  257. movl saved_video_mode, %edx
  258. movl %edx, video_mode - wakeup_start (,%rdi)
  259. movl acpi_realmode_flags, %edx
  260. movl %edx, realmode_flags - wakeup_start (,%rdi)
  261. movq $0x12345678, real_magic - wakeup_start (,%rdi)
  262. movq $0x123456789abcdef0, %rdx
  263. movq %rdx, saved_magic
  264. movq saved_magic, %rax
  265. movq $0x123456789abcdef0, %rdx
  266. cmpq %rdx, %rax
  267. jne bogus_64_magic
  268. # restore the regs we used
  269. popq %rdx
  270. popq %rax
  271. ENTRY(do_suspend_lowlevel_s4bios)
  272. ret
  273. .align 2
  274. .p2align 4,,15
  275. .globl do_suspend_lowlevel
  276. .type do_suspend_lowlevel,@function
  277. do_suspend_lowlevel:
  278. .LFB5:
  279. subq $8, %rsp
  280. xorl %eax, %eax
  281. call save_processor_state
  282. movq %rsp, saved_context_esp(%rip)
  283. movq %rax, saved_context_eax(%rip)
  284. movq %rbx, saved_context_ebx(%rip)
  285. movq %rcx, saved_context_ecx(%rip)
  286. movq %rdx, saved_context_edx(%rip)
  287. movq %rbp, saved_context_ebp(%rip)
  288. movq %rsi, saved_context_esi(%rip)
  289. movq %rdi, saved_context_edi(%rip)
  290. movq %r8, saved_context_r08(%rip)
  291. movq %r9, saved_context_r09(%rip)
  292. movq %r10, saved_context_r10(%rip)
  293. movq %r11, saved_context_r11(%rip)
  294. movq %r12, saved_context_r12(%rip)
  295. movq %r13, saved_context_r13(%rip)
  296. movq %r14, saved_context_r14(%rip)
  297. movq %r15, saved_context_r15(%rip)
  298. pushfq ; popq saved_context_eflags(%rip)
  299. movq $.L97, saved_rip(%rip)
  300. movq %rsp,saved_rsp
  301. movq %rbp,saved_rbp
  302. movq %rbx,saved_rbx
  303. movq %rdi,saved_rdi
  304. movq %rsi,saved_rsi
  305. addq $8, %rsp
  306. movl $3, %edi
  307. xorl %eax, %eax
  308. jmp acpi_enter_sleep_state
  309. .L97:
  310. .p2align 4,,7
  311. .L99:
  312. .align 4
  313. movl $24, %eax
  314. movw %ax, %ds
  315. movq saved_context+58(%rip), %rax
  316. movq %rax, %cr4
  317. movq saved_context+50(%rip), %rax
  318. movq %rax, %cr3
  319. movq saved_context+42(%rip), %rax
  320. movq %rax, %cr2
  321. movq saved_context+34(%rip), %rax
  322. movq %rax, %cr0
  323. pushq saved_context_eflags(%rip) ; popfq
  324. movq saved_context_esp(%rip), %rsp
  325. movq saved_context_ebp(%rip), %rbp
  326. movq saved_context_eax(%rip), %rax
  327. movq saved_context_ebx(%rip), %rbx
  328. movq saved_context_ecx(%rip), %rcx
  329. movq saved_context_edx(%rip), %rdx
  330. movq saved_context_esi(%rip), %rsi
  331. movq saved_context_edi(%rip), %rdi
  332. movq saved_context_r08(%rip), %r8
  333. movq saved_context_r09(%rip), %r9
  334. movq saved_context_r10(%rip), %r10
  335. movq saved_context_r11(%rip), %r11
  336. movq saved_context_r12(%rip), %r12
  337. movq saved_context_r13(%rip), %r13
  338. movq saved_context_r14(%rip), %r14
  339. movq saved_context_r15(%rip), %r15
  340. xorl %eax, %eax
  341. addq $8, %rsp
  342. jmp restore_processor_state
  343. .LFE5:
  344. .Lfe5:
  345. .size do_suspend_lowlevel,.Lfe5-do_suspend_lowlevel
  346. .data
  347. ALIGN
  348. ENTRY(saved_rbp) .quad 0
  349. ENTRY(saved_rsi) .quad 0
  350. ENTRY(saved_rdi) .quad 0
  351. ENTRY(saved_rbx) .quad 0
  352. ENTRY(saved_rip) .quad 0
  353. ENTRY(saved_rsp) .quad 0
  354. ENTRY(saved_magic) .quad 0