1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright IBM Corp. 2018
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
53 12: MEMCPY \buf,\dst,%r4
65 .macro START_NEXT_KERNEL base subcode
66 lg %r4,kernel_entry-\base(%r13)
67 lg %r5,load_psw_mask-\base(%r13)
78 ENTRY(purgatory_start)
79 /* The purgatory might be called after a diag308 so better set
80 * architecture and addressing mode.
83 sigp %r1,%r0,SIGP_SET_ARCHITECTURE
93 larl %r15,purgatory_end-STACK_FRAME_OVERHEAD
95 /* If the next kernel is KEXEC_TYPE_CRASH the purgatory is called
96 * directly with a flag passed in %r2 whether the purgatory shall do
97 * checksum verification only (%r2 = 0 -> verification only).
99 * Check now and preserve over C function call by storing in
101 * 1 -> checksum verification only
102 * 0 -> load new kernel
105 lg %r11,kernel_type-.base_crash(%r13)
106 cghi %r11,1 /* KEXEC_TYPE_CRASH */
107 jne .do_checksum_verification
108 cghi %r2,0 /* checksum verification only */
109 jne .do_checksum_verification
112 .do_checksum_verification:
113 brasl %r14,verify_sha256_digest
115 cghi %r10,1 /* checksum verification only */
116 je .return_old_kernel
117 cghi %r2,0 /* checksum match */
120 /* If the next kernel is a crash kernel the purgatory has to swap
121 * the mem regions first.
123 cghi %r11,1 /* KEXEC_TYPE_CRASH */
124 je .start_crash_kernel
126 /* start normal kernel */
127 START_NEXT_KERNEL .base_crash 0
130 lmg %r6,%r15,gprregs-.base_crash(%r13)
134 lpswe disabled_wait_psw-.base_crash(%r13)
137 /* Location of purgatory_start in crash memory */
139 aghi %r8,-(.base_crash-purgatory_start)
141 /* Destination for this code i.e. end of memory to be swapped. */
142 lg %r9,crash_size-.base_crash(%r13)
143 aghi %r9,-(purgatory_end-purgatory_start)
145 /* Destination in crash memory, i.e. same as r9 but in crash memory. */
146 lg %r10,crash_start-.base_crash(%r13)
149 /* Buffer location (in crash memory) and size. As the purgatory is
150 * behind the point of no return it can re-use the stack as buffer.
155 MEMCPY %r12,%r9,%r11 /* dst -> (crash) buf */
156 MEMCPY %r9,%r8,%r11 /* self -> dst */
158 /* Jump to new location. */
160 aghi %r7,.jump_to_dst-purgatory_start
168 MEMCPY %r12,%r10,%r11 /* (crash) buf -> (crash) dst */
170 /* Load new buffer location after jump */
172 aghi %r10,stack-purgatory_start
173 MEMCPY %r10,%r7,%r11 /* (new) buf -> (crash) buf */
175 /* Now the code is set up to run from its designated location. Start
176 * swapping the rest of crash memory now.
178 * The registers will be used as follow:
180 * %r0-%r4 reserved for macros defined above
181 * %r5-%r6 tmp registers
182 * %r7 pointer to current struct sha region
183 * %r8 index to iterate over all sha regions
184 * %r9 pointer in crash memory
185 * %r10 pointer in old kernel
186 * %r11 total size (still) to be moved
187 * %r12 pointer to buffer
192 lg %r9,crash_start-.base_dst(%r13)
193 lghi %r8,16 /* KEXEC_SEGMENTS_MAX */
194 larl %r7,purgatory_sha_regions
198 /* Loop over all purgatory_sha_regions. */
204 aghi %r7,__KEXEC_SHA_REGION_SIZE
207 lg %r5,__KEXEC_SHA_REGION_START(%r7)
211 /* Copy [end last sha region, start current sha region) */
212 /* Note: kexec_sha_region->start points in crash memory */
220 /* Swap sha region */
221 lg %r6,__KEXEC_SHA_REGION_LEN(%r7)
222 MEMSWAP %r9,%r10,%r12,%r6
223 sg %r11,__KEXEC_SHA_REGION_LEN(%r7)
227 /* Copy rest of crash memory */
230 /* start crash kernel */
231 START_NEXT_KERNEL .base_dst 1
235 .long 0x00080000,0x80000000
239 .quad 0x0002000180000000
240 .quad 0x0000000000000000 + .do_checksum_verification
247 /* Macro to define a global variable with name and size (in bytes) to be
248 * shared with C code.
250 * Add the .size and .type attribute to satisfy checks on the Elf_Sym during
253 .macro GLOBAL_VARIABLE name,size
261 GLOBAL_VARIABLE purgatory_sha256_digest,32
262 GLOBAL_VARIABLE purgatory_sha_regions,16*__KEXEC_SHA_REGION_SIZE
263 GLOBAL_VARIABLE kernel_entry,8
264 GLOBAL_VARIABLE kernel_type,8
265 GLOBAL_VARIABLE crash_start,8
266 GLOBAL_VARIABLE crash_size,8
270 /* The buffer to move this code must be as big as the code. */
271 .skip stack-purgatory_start