head.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Purgatory setup code
  4. *
  5. * Copyright IBM Corp. 2018
  6. *
  7. * Author(s): Philipp Rudo <[email protected]>
  8. */
  9. #include <linux/linkage.h>
  10. #include <asm/asm-offsets.h>
  11. #include <asm/page.h>
  12. #include <asm/sigp.h>
  13. #include <asm/ptrace.h>
  14. /* The purgatory is the code running between two kernels. It's main purpose
  15. * is to verify that the next kernel was not corrupted after load and to
  16. * start it.
  17. *
  18. * If the next kernel is a crash kernel there are some peculiarities to
  19. * consider:
  20. *
  21. * First the purgatory is called twice. Once only to verify the
  22. * sha digest. So if the crash kernel got corrupted the old kernel can try
  23. * to trigger a stand-alone dumper. And once to actually load the crash kernel.
  24. *
  25. * Second the purgatory also has to swap the crash memory region with its
  26. * destination at address 0. As the purgatory is part of crash memory this
  27. * requires some finesse. The tactic here is that the purgatory first copies
  28. * itself to the end of the destination and then swaps the rest of the
  29. * memory running from there.
  30. */
  31. #define bufsz purgatory_end-stack
  32. .macro MEMCPY dst,src,len
  33. lgr %r0,\dst
  34. lgr %r1,\len
  35. lgr %r2,\src
  36. lgr %r3,\len
  37. 20: mvcle %r0,%r2,0
  38. jo 20b
  39. .endm
  40. .macro MEMSWAP dst,src,buf,len
  41. 10: larl %r0,purgatory_end
  42. larl %r1,stack
  43. slgr %r0,%r1
  44. cgr \len,%r0
  45. jh 11f
  46. lgr %r4,\len
  47. j 12f
  48. 11: lgr %r4,%r0
  49. 12: MEMCPY \buf,\dst,%r4
  50. MEMCPY \dst,\src,%r4
  51. MEMCPY \src,\buf,%r4
  52. agr \dst,%r4
  53. agr \src,%r4
  54. sgr \len,%r4
  55. cghi \len,0
  56. jh 10b
  57. .endm
  58. .macro START_NEXT_KERNEL base subcode
  59. lg %r4,kernel_entry-\base(%r13)
  60. lg %r5,load_psw_mask-\base(%r13)
  61. ogr %r4,%r5
  62. stg %r4,0(%r0)
  63. xgr %r0,%r0
  64. lghi %r1,\subcode
  65. diag %r0,%r1,0x308
  66. .endm
  67. .text
  68. .align PAGE_SIZE
  69. ENTRY(purgatory_start)
  70. /* The purgatory might be called after a diag308 so better set
  71. * architecture and addressing mode.
  72. */
  73. lhi %r1,1
  74. sigp %r1,%r0,SIGP_SET_ARCHITECTURE
  75. sam64
  76. larl %r5,gprregs
  77. stmg %r6,%r15,0(%r5)
  78. basr %r13,0
  79. .base_crash:
  80. /* Setup stack */
  81. larl %r15,purgatory_end-STACK_FRAME_OVERHEAD
  82. /* If the next kernel is KEXEC_TYPE_CRASH the purgatory is called
  83. * directly with a flag passed in %r2 whether the purgatory shall do
  84. * checksum verification only (%r2 = 0 -> verification only).
  85. *
  86. * Check now and preserve over C function call by storing in
  87. * %r10 whith
  88. * 1 -> checksum verification only
  89. * 0 -> load new kernel
  90. */
  91. lghi %r10,0
  92. lg %r11,kernel_type-.base_crash(%r13)
  93. cghi %r11,1 /* KEXEC_TYPE_CRASH */
  94. jne .do_checksum_verification
  95. cghi %r2,0 /* checksum verification only */
  96. jne .do_checksum_verification
  97. lghi %r10,1
  98. .do_checksum_verification:
  99. brasl %r14,verify_sha256_digest
  100. cghi %r10,1 /* checksum verification only */
  101. je .return_old_kernel
  102. cghi %r2,0 /* checksum match */
  103. jne .disabled_wait
  104. /* If the next kernel is a crash kernel the purgatory has to swap
  105. * the mem regions first.
  106. */
  107. cghi %r11,1 /* KEXEC_TYPE_CRASH */
  108. je .start_crash_kernel
  109. /* start normal kernel */
  110. START_NEXT_KERNEL .base_crash 0
  111. .return_old_kernel:
  112. lmg %r6,%r15,gprregs-.base_crash(%r13)
  113. br %r14
  114. .disabled_wait:
  115. lpswe disabled_wait_psw-.base_crash(%r13)
  116. .start_crash_kernel:
  117. /* Location of purgatory_start in crash memory */
  118. larl %r0,.base_crash
  119. larl %r1,purgatory_start
  120. slgr %r0,%r1
  121. lgr %r8,%r13
  122. sgr %r8,%r0
  123. /* Destination for this code i.e. end of memory to be swapped. */
  124. larl %r0,purgatory_end
  125. larl %r1,purgatory_start
  126. slgr %r0,%r1
  127. lg %r9,crash_size-.base_crash(%r13)
  128. sgr %r9,%r0
  129. /* Destination in crash memory, i.e. same as r9 but in crash memory. */
  130. lg %r10,crash_start-.base_crash(%r13)
  131. agr %r10,%r9
  132. /* Buffer location (in crash memory) and size. As the purgatory is
  133. * behind the point of no return it can re-use the stack as buffer.
  134. */
  135. larl %r11,purgatory_end
  136. larl %r12,stack
  137. slgr %r11,%r12
  138. MEMCPY %r12,%r9,%r11 /* dst -> (crash) buf */
  139. MEMCPY %r9,%r8,%r11 /* self -> dst */
  140. /* Jump to new location. */
  141. lgr %r7,%r9
  142. larl %r0,.jump_to_dst
  143. larl %r1,purgatory_start
  144. slgr %r0,%r1
  145. agr %r7,%r0
  146. br %r7
  147. .jump_to_dst:
  148. basr %r13,0
  149. .base_dst:
  150. /* clear buffer */
  151. MEMCPY %r12,%r10,%r11 /* (crash) buf -> (crash) dst */
  152. /* Load new buffer location after jump */
  153. larl %r7,stack
  154. lgr %r0,%r7
  155. larl %r1,purgatory_start
  156. slgr %r0,%r1
  157. agr %r10,%r0
  158. MEMCPY %r10,%r7,%r11 /* (new) buf -> (crash) buf */
  159. /* Now the code is set up to run from its designated location. Start
  160. * swapping the rest of crash memory now.
  161. *
  162. * The registers will be used as follow:
  163. *
  164. * %r0-%r4 reserved for macros defined above
  165. * %r5-%r6 tmp registers
  166. * %r7 pointer to current struct sha region
  167. * %r8 index to iterate over all sha regions
  168. * %r9 pointer in crash memory
  169. * %r10 pointer in old kernel
  170. * %r11 total size (still) to be moved
  171. * %r12 pointer to buffer
  172. */
  173. lgr %r12,%r7
  174. lgr %r11,%r9
  175. lghi %r10,0
  176. lg %r9,crash_start-.base_dst(%r13)
  177. lghi %r8,16 /* KEXEC_SEGMENTS_MAX */
  178. larl %r7,purgatory_sha_regions
  179. j .loop_first
  180. /* Loop over all purgatory_sha_regions. */
  181. .loop_next:
  182. aghi %r8,-1
  183. cghi %r8,0
  184. je .loop_out
  185. aghi %r7,__KEXEC_SHA_REGION_SIZE
  186. .loop_first:
  187. lg %r5,__KEXEC_SHA_REGION_START(%r7)
  188. cghi %r5,0
  189. je .loop_next
  190. /* Copy [end last sha region, start current sha region) */
  191. /* Note: kexec_sha_region->start points in crash memory */
  192. sgr %r5,%r9
  193. MEMCPY %r9,%r10,%r5
  194. agr %r9,%r5
  195. agr %r10,%r5
  196. sgr %r11,%r5
  197. /* Swap sha region */
  198. lg %r6,__KEXEC_SHA_REGION_LEN(%r7)
  199. MEMSWAP %r9,%r10,%r12,%r6
  200. sg %r11,__KEXEC_SHA_REGION_LEN(%r7)
  201. j .loop_next
  202. .loop_out:
  203. /* Copy rest of crash memory */
  204. MEMCPY %r9,%r10,%r11
  205. /* start crash kernel */
  206. START_NEXT_KERNEL .base_dst 1
  207. load_psw_mask:
  208. .long 0x00080000,0x80000000
  209. .align 8
  210. disabled_wait_psw:
  211. .quad 0x0002000180000000
  212. .quad 0x0000000000000000 + .do_checksum_verification
  213. gprregs:
  214. .rept 10
  215. .quad 0
  216. .endr
  217. /* Macro to define a global variable with name and size (in bytes) to be
  218. * shared with C code.
  219. *
  220. * Add the .size and .type attribute to satisfy checks on the Elf_Sym during
  221. * purgatory load.
  222. */
  223. .macro GLOBAL_VARIABLE name,size
  224. \name:
  225. .global \name
  226. .size \name,\size
  227. .type \name,object
  228. .skip \size,0
  229. .endm
  230. GLOBAL_VARIABLE purgatory_sha256_digest,32
  231. GLOBAL_VARIABLE purgatory_sha_regions,16*__KEXEC_SHA_REGION_SIZE
  232. GLOBAL_VARIABLE kernel_entry,8
  233. GLOBAL_VARIABLE kernel_type,8
  234. GLOBAL_VARIABLE crash_start,8
  235. GLOBAL_VARIABLE crash_size,8
  236. .align PAGE_SIZE
  237. stack:
  238. /* The buffer to move this code must be as big as the code. */
  239. .skip stack-purgatory_start
  240. .align PAGE_SIZE
  241. purgatory_end: