wakeup.S 10.0 KB

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