1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright IBM Corp. 2018
7 * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com>
10 #include <linux/linkage.h>
11 #include <asm/asm-offsets.h>
14 #include <asm/ptrace.h>
16 /* The purgatory is the code running between two kernels. It's main purpose
17 * is to verify that the next kernel was not corrupted after load and to
20 * If the next kernel is a crash kernel there are some peculiarities to
23 * First the purgatory is called twice. Once only to verify the
24 * sha digest. So if the crash kernel got corrupted the old kernel can try
25 * to trigger a stand-alone dumper. And once to actually load the crash kernel.
27 * Second the purgatory also has to swap the crash memory region with its
28 * destination at address 0. As the purgatory is part of crash memory this
29 * requires some finesse. The tactic here is that the purgatory first copies
30 * itself to the end of the destination and then swaps the rest of the
31 * memory running from there.
34 #define bufsz purgatory_end-stack
36 .macro MEMCPY dst,src,len
46 .macro MEMSWAP dst,src,buf,len
47 10: larl %r0,purgatory_end
56 12: MEMCPY \buf,\dst,%r4
68 .macro START_NEXT_KERNEL base subcode
69 lg %r4,kernel_entry-\base(%r13)
70 lg %r5,load_psw_mask-\base(%r13)
81 SYM_CODE_START(purgatory_start)
82 /* The purgatory might be called after a diag308 so better set
83 * architecture and addressing mode.
86 sigp %r1,%r0,SIGP_SET_ARCHITECTURE
96 larl %r15,purgatory_end-STACK_FRAME_OVERHEAD
98 /* If the next kernel is KEXEC_TYPE_CRASH the purgatory is called
99 * directly with a flag passed in %r2 whether the purgatory shall do
100 * checksum verification only (%r2 = 0 -> verification only).
102 * Check now and preserve over C function call by storing in
104 * 1 -> checksum verification only
105 * 0 -> load new kernel
108 lg %r11,kernel_type-.base_crash(%r13)
109 cghi %r11,1 /* KEXEC_TYPE_CRASH */
110 jne .do_checksum_verification
111 cghi %r2,0 /* checksum verification only */
112 jne .do_checksum_verification
115 .do_checksum_verification:
116 brasl %r14,verify_sha256_digest
118 cghi %r10,1 /* checksum verification only */
119 je .return_old_kernel
120 cghi %r2,0 /* checksum match */
123 /* If the next kernel is a crash kernel the purgatory has to swap
124 * the mem regions first.
126 cghi %r11,1 /* KEXEC_TYPE_CRASH */
127 je .start_crash_kernel
129 /* start normal kernel */
130 START_NEXT_KERNEL .base_crash 0
133 lmg %r6,%r15,gprregs-.base_crash(%r13)
137 lpswe disabled_wait_psw-.base_crash(%r13)
140 /* Location of purgatory_start in crash memory */
142 larl %r1,purgatory_start
147 /* Destination for this code i.e. end of memory to be swapped. */
148 larl %r0,purgatory_end
149 larl %r1,purgatory_start
151 lg %r9,crash_size-.base_crash(%r13)
154 /* Destination in crash memory, i.e. same as r9 but in crash memory. */
155 lg %r10,crash_start-.base_crash(%r13)
158 /* Buffer location (in crash memory) and size. As the purgatory is
159 * behind the point of no return it can re-use the stack as buffer.
161 larl %r11,purgatory_end
165 MEMCPY %r12,%r9,%r11 /* dst -> (crash) buf */
166 MEMCPY %r9,%r8,%r11 /* self -> dst */
168 /* Jump to new location. */
170 larl %r0,.jump_to_dst
171 larl %r1,purgatory_start
181 MEMCPY %r12,%r10,%r11 /* (crash) buf -> (crash) dst */
183 /* Load new buffer location after jump */
186 larl %r1,purgatory_start
189 MEMCPY %r10,%r7,%r11 /* (new) buf -> (crash) buf */
191 /* Now the code is set up to run from its designated location. Start
192 * swapping the rest of crash memory now.
194 * The registers will be used as follow:
196 * %r0-%r4 reserved for macros defined above
197 * %r5-%r6 tmp registers
198 * %r7 pointer to current struct sha region
199 * %r8 index to iterate over all sha regions
200 * %r9 pointer in crash memory
201 * %r10 pointer in old kernel
202 * %r11 total size (still) to be moved
203 * %r12 pointer to buffer
208 lg %r9,crash_start-.base_dst(%r13)
209 lghi %r8,16 /* KEXEC_SEGMENTS_MAX */
210 larl %r7,purgatory_sha_regions
214 /* Loop over all purgatory_sha_regions. */
220 aghi %r7,__KEXEC_SHA_REGION_SIZE
223 lg %r5,__KEXEC_SHA_REGION_START(%r7)
227 /* Copy [end last sha region, start current sha region) */
228 /* Note: kexec_sha_region->start points in crash memory */
236 /* Swap sha region */
237 lg %r6,__KEXEC_SHA_REGION_LEN(%r7)
238 MEMSWAP %r9,%r10,%r12,%r6
239 sg %r11,__KEXEC_SHA_REGION_LEN(%r7)
243 /* Copy rest of crash memory */
246 /* start crash kernel */
247 START_NEXT_KERNEL .base_dst 1
248 SYM_CODE_END(purgatory_start)
250 SYM_DATA_LOCAL(load_psw_mask, .long 0x00080000,0x80000000)
252 SYM_DATA_LOCAL(disabled_wait_psw, .quad 0x0002000180000000,.do_checksum_verification)
253 SYM_DATA_LOCAL(gprregs, .fill 10,8,0)
254 SYM_DATA(purgatory_sha256_digest, .skip 32)
255 SYM_DATA(purgatory_sha_regions, .skip 16*__KEXEC_SHA_REGION_SIZE)
256 SYM_DATA(kernel_entry, .skip 8)
257 SYM_DATA(kernel_type, .skip 8)
258 SYM_DATA(crash_start, .skip 8)
259 SYM_DATA(crash_size, .skip 8)
261 SYM_DATA_START_LOCAL(stack)
262 /* The buffer to move this code must be as big as the code. */
263 .skip stack-purgatory_start
265 SYM_DATA_END_LABEL(stack, SYM_L_LOCAL, purgatory_end)