wakeup_64.S 9.3 KB

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