/* SPDX-License-Identifier: GPL-2.0 */1/*2* Purgatory setup code3*4* Copyright IBM Corp. 20185*6* Author(s): Philipp Rudo <[email protected]>7*/89#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>1415/* The purgatory is the code running between two kernels. It's main purpose16* is to verify that the next kernel was not corrupted after load and to17* start it.18*19* If the next kernel is a crash kernel there are some peculiarities to20* consider:21*22* First the purgatory is called twice. Once only to verify the23* sha digest. So if the crash kernel got corrupted the old kernel can try24* to trigger a stand-alone dumper. And once to actually load the crash kernel.25*26* Second the purgatory also has to swap the crash memory region with its27* destination at address 0. As the purgatory is part of crash memory this28* requires some finesse. The tactic here is that the purgatory first copies29* itself to the end of the destination and then swaps the rest of the30* memory running from there.31*/3233#define bufsz purgatory_end-stack3435.macro MEMCPY dst,src,len36lgr %r0,\dst37lgr %r1,\len38lgr %r2,\src39lgr %r3,\len404120: mvcle %r0,%r2,042jo 20b43.endm4445.macro MEMSWAP dst,src,buf,len4610: larl %r0,purgatory_end47larl %r1,stack48slgr %r0,%r149cgr \len,%r050jh 11f51lgr %r4,\len52j 12f5311: lgr %r4,%r0545512: MEMCPY \buf,\dst,%r456MEMCPY \dst,\src,%r457MEMCPY \src,\buf,%r45859agr \dst,%r460agr \src,%r461sgr \len,%r46263cghi \len,064jh 10b65.endm6667.macro START_NEXT_KERNEL base subcode68lg %r4,kernel_entry-\base(%r13)69lg %r5,load_psw_mask-\base(%r13)70ogr %r4,%r571stg %r4,0(%r0)7273xgr %r0,%r074lghi %r1,\subcode75diag %r0,%r1,0x30876.endm7778.text79.balign PAGE_SIZE80SYM_CODE_START(purgatory_start)81/* The purgatory might be called after a diag308 so better set82* architecture and addressing mode.83*/84lhi %r1,185sigp %r1,%r0,SIGP_SET_ARCHITECTURE86sam648788larl %r5,gprregs89stmg %r6,%r15,0(%r5)9091basr %r13,092.base_crash:9394/* Setup stack */95larl %r15,purgatory_end-STACK_FRAME_OVERHEAD9697/* If the next kernel is KEXEC_TYPE_CRASH the purgatory is called98* directly with a flag passed in %r2 whether the purgatory shall do99* checksum verification only (%r2 = 0 -> verification only).100*101* Check now and preserve over C function call by storing in102* %r10 with103* 1 -> checksum verification only104* 0 -> load new kernel105*/106lghi %r10,0107lg %r11,kernel_type-.base_crash(%r13)108cghi %r11,1 /* KEXEC_TYPE_CRASH */109jne .do_checksum_verification110cghi %r2,0 /* checksum verification only */111jne .do_checksum_verification112lghi %r10,1113114.do_checksum_verification:115brasl %r14,verify_sha256_digest116117cghi %r10,1 /* checksum verification only */118je .return_old_kernel119cghi %r2,0 /* checksum match */120jne .disabled_wait121122/* If the next kernel is a crash kernel the purgatory has to swap123* the mem regions first.124*/125cghi %r11,1 /* KEXEC_TYPE_CRASH */126je .start_crash_kernel127128/* start normal kernel */129START_NEXT_KERNEL .base_crash 0130131.return_old_kernel:132lmg %r6,%r15,gprregs-.base_crash(%r13)133br %r14134135.disabled_wait:136lpswe disabled_wait_psw-.base_crash(%r13)137138.start_crash_kernel:139/* Location of purgatory_start in crash memory */140larl %r0,.base_crash141larl %r1,purgatory_start142slgr %r0,%r1143lgr %r8,%r13144sgr %r8,%r0145146/* Destination for this code i.e. end of memory to be swapped. */147larl %r0,purgatory_end148larl %r1,purgatory_start149slgr %r0,%r1150lg %r9,crash_size-.base_crash(%r13)151sgr %r9,%r0152153/* Destination in crash memory, i.e. same as r9 but in crash memory. */154lg %r10,crash_start-.base_crash(%r13)155agr %r10,%r9156157/* Buffer location (in crash memory) and size. As the purgatory is158* behind the point of no return it can reuse the stack as buffer.159*/160larl %r11,purgatory_end161larl %r12,stack162slgr %r11,%r12163164MEMCPY %r12,%r9,%r11 /* dst -> (crash) buf */165MEMCPY %r9,%r8,%r11 /* self -> dst */166167/* Jump to new location. */168lgr %r7,%r9169larl %r0,.jump_to_dst170larl %r1,purgatory_start171slgr %r0,%r1172agr %r7,%r0173br %r7174175.jump_to_dst:176basr %r13,0177.base_dst:178179/* clear buffer */180MEMCPY %r12,%r10,%r11 /* (crash) buf -> (crash) dst */181182/* Load new buffer location after jump */183larl %r7,stack184lgr %r0,%r7185larl %r1,purgatory_start186slgr %r0,%r1187agr %r10,%r0188MEMCPY %r10,%r7,%r11 /* (new) buf -> (crash) buf */189190/* Now the code is set up to run from its designated location. Start191* swapping the rest of crash memory now.192*193* The registers will be used as follow:194*195* %r0-%r4 reserved for macros defined above196* %r5-%r6 tmp registers197* %r7 pointer to current struct sha region198* %r8 index to iterate over all sha regions199* %r9 pointer in crash memory200* %r10 pointer in old kernel201* %r11 total size (still) to be moved202* %r12 pointer to buffer203*/204lgr %r12,%r7205lgr %r11,%r9206lghi %r10,0207lg %r9,crash_start-.base_dst(%r13)208lghi %r8,16 /* KEXEC_SEGMENTS_MAX */209larl %r7,purgatory_sha_regions210211j .loop_first212213/* Loop over all purgatory_sha_regions. */214.loop_next:215aghi %r8,-1216cghi %r8,0217je .loop_out218219aghi %r7,__KEXEC_SHA_REGION_SIZE220221.loop_first:222lg %r5,__KEXEC_SHA_REGION_START(%r7)223cghi %r5,0224je .loop_next225226/* Copy [end last sha region, start current sha region) */227/* Note: kexec_sha_region->start points in crash memory */228sgr %r5,%r9229MEMCPY %r9,%r10,%r5230231agr %r9,%r5232agr %r10,%r5233sgr %r11,%r5234235/* Swap sha region */236lg %r6,__KEXEC_SHA_REGION_LEN(%r7)237MEMSWAP %r9,%r10,%r12,%r6238sg %r11,__KEXEC_SHA_REGION_LEN(%r7)239j .loop_next240241.loop_out:242/* Copy rest of crash memory */243MEMCPY %r9,%r10,%r11244245/* start crash kernel */246START_NEXT_KERNEL .base_dst 1247SYM_CODE_END(purgatory_start)248249SYM_DATA_LOCAL(load_psw_mask, .long 0x00080000,0x80000000)250.balign 8251SYM_DATA_LOCAL(disabled_wait_psw, .quad 0x0002000180000000,.do_checksum_verification)252SYM_DATA_LOCAL(gprregs, .fill 10,8,0)253SYM_DATA(purgatory_sha256_digest, .skip 32)254SYM_DATA(purgatory_sha_regions, .skip 16*__KEXEC_SHA_REGION_SIZE)255SYM_DATA(kernel_entry, .skip 8)256SYM_DATA(kernel_type, .skip 8)257SYM_DATA(crash_start, .skip 8)258SYM_DATA(crash_size, .skip 8)259.balign PAGE_SIZE260SYM_DATA_START_LOCAL(stack)261/* The buffer to move this code must be as big as the code. */262.skip stack-purgatory_start263.balign PAGE_SIZE264SYM_DATA_END_LABEL(stack, SYM_L_LOCAL, purgatory_end)265266267