wakeup.S 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. .text
  2. #include <linux/linkage.h>
  3. #include <asm/segment.h>
  4. #include <asm/page.h>
  5. #
  6. # wakeup_code runs in real mode, and at unknown address (determined at run-time).
  7. # Therefore it must only use relative jumps/calls.
  8. #
  9. # Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
  10. #
  11. # If physical address of wakeup_code is 0x12345, BIOS should call us with
  12. # cs = 0x1234, eip = 0x05
  13. #
  14. #define BEEP \
  15. inb $97, %al; \
  16. outb %al, $0x80; \
  17. movb $3, %al; \
  18. outb %al, $97; \
  19. outb %al, $0x80; \
  20. movb $-74, %al; \
  21. outb %al, $67; \
  22. outb %al, $0x80; \
  23. movb $-119, %al; \
  24. outb %al, $66; \
  25. outb %al, $0x80; \
  26. movb $15, %al; \
  27. outb %al, $66;
  28. ALIGN
  29. .align 4096
  30. ENTRY(wakeup_start)
  31. wakeup_code:
  32. wakeup_code_start = .
  33. .code16
  34. movw $0xb800, %ax
  35. movw %ax,%fs
  36. movw $0x0e00 + 'L', %fs:(0x10)
  37. cli
  38. cld
  39. # setup data segment
  40. movw %cs, %ax
  41. movw %ax, %ds # Make ds:0 point to wakeup_start
  42. movw %ax, %ss
  43. testl $4, realmode_flags - wakeup_code
  44. jz 1f
  45. BEEP
  46. 1:
  47. mov $(wakeup_stack - wakeup_code), %sp # Private stack is needed for ASUS board
  48. movw $0x0e00 + 'S', %fs:(0x12)
  49. pushl $0 # Kill any dangerous flags
  50. popfl
  51. movl real_magic - wakeup_code, %eax
  52. cmpl $0x12345678, %eax
  53. jne bogus_real_magic
  54. testl $1, realmode_flags - wakeup_code
  55. jz 1f
  56. lcall $0xc000,$3
  57. movw %cs, %ax
  58. movw %ax, %ds # Bios might have played with that
  59. movw %ax, %ss
  60. 1:
  61. testl $2, realmode_flags - wakeup_code
  62. jz 1f
  63. mov video_mode - wakeup_code, %ax
  64. call mode_set
  65. 1:
  66. # set up page table
  67. movl $swsusp_pg_dir-__PAGE_OFFSET, %eax
  68. movl %eax, %cr3
  69. testl $1, real_efer_save_restore - wakeup_code
  70. jz 4f
  71. # restore efer setting
  72. movl real_save_efer_edx - wakeup_code, %edx
  73. movl real_save_efer_eax - wakeup_code, %eax
  74. mov $0xc0000080, %ecx
  75. wrmsr
  76. 4:
  77. # make sure %cr4 is set correctly (features, etc)
  78. movl real_save_cr4 - wakeup_code, %eax
  79. movl %eax, %cr4
  80. movw $0xb800, %ax
  81. movw %ax,%fs
  82. movw $0x0e00 + 'i', %fs:(0x12)
  83. # need a gdt -- use lgdtl to force 32-bit operands, in case
  84. # the GDT is located past 16 megabytes.
  85. lgdtl real_save_gdt - wakeup_code
  86. movl real_save_cr0 - wakeup_code, %eax
  87. movl %eax, %cr0
  88. jmp 1f
  89. 1:
  90. movw $0x0e00 + 'n', %fs:(0x14)
  91. movl real_magic - wakeup_code, %eax
  92. cmpl $0x12345678, %eax
  93. jne bogus_real_magic
  94. testl $8, realmode_flags - wakeup_code
  95. jz 1f
  96. BEEP
  97. 1:
  98. ljmpl $__KERNEL_CS, $wakeup_pmode_return
  99. real_save_gdt: .word 0
  100. .long 0
  101. real_save_cr0: .long 0
  102. real_save_cr3: .long 0
  103. real_save_cr4: .long 0
  104. real_magic: .long 0
  105. video_mode: .long 0
  106. realmode_flags: .long 0
  107. beep_flags: .long 0
  108. real_efer_save_restore: .long 0
  109. real_save_efer_edx: .long 0
  110. real_save_efer_eax: .long 0
  111. bogus_real_magic:
  112. movw $0x0e00 + 'B', %fs:(0x12)
  113. jmp bogus_real_magic
  114. /* This code uses an extended set of video mode numbers. These include:
  115. * Aliases for standard modes
  116. * NORMAL_VGA (-1)
  117. * EXTENDED_VGA (-2)
  118. * ASK_VGA (-3)
  119. * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
  120. * of compatibility when extending the table. These are between 0x00 and 0xff.
  121. */
  122. #define VIDEO_FIRST_MENU 0x0000
  123. /* Standard BIOS video modes (BIOS number + 0x0100) */
  124. #define VIDEO_FIRST_BIOS 0x0100
  125. /* VESA BIOS video modes (VESA number + 0x0200) */
  126. #define VIDEO_FIRST_VESA 0x0200
  127. /* Video7 special modes (BIOS number + 0x0900) */
  128. #define VIDEO_FIRST_V7 0x0900
  129. # Setting of user mode (AX=mode ID) => CF=success
  130. mode_set:
  131. movw %ax, %bx
  132. #if 0
  133. cmpb $0xff, %ah
  134. jz setalias
  135. testb $VIDEO_RECALC>>8, %ah
  136. jnz _setrec
  137. cmpb $VIDEO_FIRST_RESOLUTION>>8, %ah
  138. jnc setres
  139. cmpb $VIDEO_FIRST_SPECIAL>>8, %ah
  140. jz setspc
  141. cmpb $VIDEO_FIRST_V7>>8, %ah
  142. jz setv7
  143. #endif
  144. cmpb $VIDEO_FIRST_VESA>>8, %ah
  145. jnc check_vesa
  146. #if 0
  147. orb %ah, %ah
  148. jz setmenu
  149. #endif
  150. decb %ah
  151. # jz setbios Add bios modes later
  152. setbad: clc
  153. ret
  154. check_vesa:
  155. subb $VIDEO_FIRST_VESA>>8, %bh
  156. orw $0x4000, %bx # Use linear frame buffer
  157. movw $0x4f02, %ax # VESA BIOS mode set call
  158. int $0x10
  159. cmpw $0x004f, %ax # AL=4f if implemented
  160. jnz _setbad # AH=0 if OK
  161. stc
  162. ret
  163. _setbad: jmp setbad
  164. .code32
  165. ALIGN
  166. .org 0x800
  167. wakeup_stack_begin: # Stack grows down
  168. .org 0xff0 # Just below end of page
  169. wakeup_stack:
  170. ENTRY(wakeup_end)
  171. .org 0x1000
  172. wakeup_pmode_return:
  173. movw $__KERNEL_DS, %ax
  174. movw %ax, %ss
  175. movw %ax, %ds
  176. movw %ax, %es
  177. movw %ax, %fs
  178. movw %ax, %gs
  179. movw $0x0e00 + 'u', 0xb8016
  180. # reload the gdt, as we need the full 32 bit address
  181. lgdt saved_gdt
  182. lidt saved_idt
  183. lldt saved_ldt
  184. ljmp $(__KERNEL_CS),$1f
  185. 1:
  186. movl %cr3, %eax
  187. movl %eax, %cr3
  188. wbinvd
  189. # and restore the stack ... but you need gdt for this to work
  190. movl saved_context_esp, %esp
  191. movl %cs:saved_magic, %eax
  192. cmpl $0x12345678, %eax
  193. jne bogus_magic
  194. # jump to place where we left off
  195. movl saved_eip,%eax
  196. jmp *%eax
  197. bogus_magic:
  198. movw $0x0e00 + 'B', 0xb8018
  199. jmp bogus_magic
  200. ##
  201. # acpi_copy_wakeup_routine
  202. #
  203. # Copy the above routine to low memory.
  204. #
  205. # Parameters:
  206. # %eax: place to copy wakeup routine to
  207. #
  208. # Returned address is location of code in low memory (past data and stack)
  209. #
  210. ENTRY(acpi_copy_wakeup_routine)
  211. pushl %ebx
  212. sgdt saved_gdt
  213. sidt saved_idt
  214. sldt saved_ldt
  215. str saved_tss
  216. movl nx_enabled, %edx
  217. movl %edx, real_efer_save_restore - wakeup_start (%eax)
  218. testl $1, real_efer_save_restore - wakeup_start (%eax)
  219. jz 2f
  220. # save efer setting
  221. pushl %eax
  222. movl %eax, %ebx
  223. mov $0xc0000080, %ecx
  224. rdmsr
  225. movl %edx, real_save_efer_edx - wakeup_start (%ebx)
  226. movl %eax, real_save_efer_eax - wakeup_start (%ebx)
  227. popl %eax
  228. 2:
  229. movl %cr3, %edx
  230. movl %edx, real_save_cr3 - wakeup_start (%eax)
  231. movl %cr4, %edx
  232. movl %edx, real_save_cr4 - wakeup_start (%eax)
  233. movl %cr0, %edx
  234. movl %edx, real_save_cr0 - wakeup_start (%eax)
  235. sgdt real_save_gdt - wakeup_start (%eax)
  236. movl saved_videomode, %edx
  237. movl %edx, video_mode - wakeup_start (%eax)
  238. movl acpi_realmode_flags, %edx
  239. movl %edx, realmode_flags - wakeup_start (%eax)
  240. movl $0x12345678, real_magic - wakeup_start (%eax)
  241. movl $0x12345678, saved_magic
  242. popl %ebx
  243. ret
  244. save_registers:
  245. leal 4(%esp), %eax
  246. movl %eax, saved_context_esp
  247. movl %ebx, saved_context_ebx
  248. movl %ebp, saved_context_ebp
  249. movl %esi, saved_context_esi
  250. movl %edi, saved_context_edi
  251. pushfl ; popl saved_context_eflags
  252. movl $ret_point, saved_eip
  253. ret
  254. restore_registers:
  255. movl saved_context_ebp, %ebp
  256. movl saved_context_ebx, %ebx
  257. movl saved_context_esi, %esi
  258. movl saved_context_edi, %edi
  259. pushl saved_context_eflags ; popfl
  260. ret
  261. ENTRY(do_suspend_lowlevel)
  262. call save_processor_state
  263. call save_registers
  264. pushl $3
  265. call acpi_enter_sleep_state
  266. addl $4, %esp
  267. # In case of S3 failure, we'll emerge here. Jump
  268. # to ret_point to recover
  269. jmp ret_point
  270. .p2align 4,,7
  271. ret_point:
  272. call restore_registers
  273. call restore_processor_state
  274. ret
  275. .data
  276. ALIGN
  277. ENTRY(saved_magic) .long 0
  278. ENTRY(saved_eip) .long 0
  279. # saved registers
  280. saved_gdt: .long 0,0
  281. saved_idt: .long 0,0
  282. saved_ldt: .long 0
  283. saved_tss: .long 0