head.S 6.1 KB

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