Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Purgatory setup code
0004  *
0005  * Copyright IBM Corp. 2018
0006  *
0007  * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com>
0008  */
0009 
0010 #include <linux/linkage.h>
0011 #include <asm/asm-offsets.h>
0012 #include <asm/page.h>
0013 #include <asm/sigp.h>
0014 #include <asm/ptrace.h>
0015 
0016 /* The purgatory is the code running between two kernels. It's main purpose
0017  * is to verify that the next kernel was not corrupted after load and to
0018  * start it.
0019  *
0020  * If the next kernel is a crash kernel there are some peculiarities to
0021  * consider:
0022  *
0023  * First the purgatory is called twice. Once only to verify the
0024  * sha digest. So if the crash kernel got corrupted the old kernel can try
0025  * to trigger a stand-alone dumper. And once to actually load the crash kernel.
0026  *
0027  * Second the purgatory also has to swap the crash memory region with its
0028  * destination at address 0. As the purgatory is part of crash memory this
0029  * requires some finesse. The tactic here is that the purgatory first copies
0030  * itself to the end of the destination and then swaps the rest of the
0031  * memory running from there.
0032  */
0033 
0034 #define bufsz purgatory_end-stack
0035 
0036 .macro MEMCPY dst,src,len
0037     lgr %r0,\dst
0038     lgr %r1,\len
0039     lgr %r2,\src
0040     lgr %r3,\len
0041 
0042 20: mvcle   %r0,%r2,0
0043     jo  20b
0044 .endm
0045 
0046 .macro MEMSWAP dst,src,buf,len
0047 10: larl    %r0,purgatory_end
0048     larl    %r1,stack
0049     slgr    %r0,%r1
0050     cgr \len,%r0
0051     jh  11f
0052     lgr %r4,\len
0053     j   12f
0054 11: lgr %r4,%r0
0055 
0056 12: MEMCPY  \buf,\dst,%r4
0057     MEMCPY  \dst,\src,%r4
0058     MEMCPY  \src,\buf,%r4
0059 
0060     agr \dst,%r4
0061     agr \src,%r4
0062     sgr \len,%r4
0063 
0064     cghi    \len,0
0065     jh  10b
0066 .endm
0067 
0068 .macro START_NEXT_KERNEL base subcode
0069     lg  %r4,kernel_entry-\base(%r13)
0070     lg  %r5,load_psw_mask-\base(%r13)
0071     ogr %r4,%r5
0072     stg %r4,0(%r0)
0073 
0074     xgr %r0,%r0
0075     lghi    %r1,\subcode
0076     diag    %r0,%r1,0x308
0077 .endm
0078 
0079 .text
0080 .align PAGE_SIZE
0081 ENTRY(purgatory_start)
0082     /* The purgatory might be called after a diag308 so better set
0083      * architecture and addressing mode.
0084      */
0085     lhi %r1,1
0086     sigp    %r1,%r0,SIGP_SET_ARCHITECTURE
0087     sam64
0088 
0089     larl    %r5,gprregs
0090     stmg    %r6,%r15,0(%r5)
0091 
0092     basr    %r13,0
0093 .base_crash:
0094 
0095     /* Setup stack */
0096     larl    %r15,purgatory_end-STACK_FRAME_OVERHEAD
0097 
0098     /* If the next kernel is KEXEC_TYPE_CRASH the purgatory is called
0099      * directly with a flag passed in %r2 whether the purgatory shall do
0100      * checksum verification only (%r2 = 0 -> verification only).
0101      *
0102      * Check now and preserve over C function call by storing in
0103      * %r10 whith
0104      *  1 -> checksum verification only
0105      *  0 -> load new kernel
0106      */
0107     lghi    %r10,0
0108     lg  %r11,kernel_type-.base_crash(%r13)
0109     cghi    %r11,1      /* KEXEC_TYPE_CRASH */
0110     jne .do_checksum_verification
0111     cghi    %r2,0       /* checksum verification only */
0112     jne .do_checksum_verification
0113     lghi    %r10,1
0114 
0115 .do_checksum_verification:
0116     brasl   %r14,verify_sha256_digest
0117 
0118     cghi    %r10,1      /* checksum verification only */
0119     je  .return_old_kernel
0120     cghi    %r2,0       /* checksum match */
0121     jne .disabled_wait
0122 
0123     /* If the next kernel is a crash kernel the purgatory has to swap
0124      * the mem regions first.
0125      */
0126     cghi    %r11,1 /* KEXEC_TYPE_CRASH */
0127     je  .start_crash_kernel
0128 
0129     /* start normal kernel */
0130     START_NEXT_KERNEL .base_crash 0
0131 
0132 .return_old_kernel:
0133     lmg %r6,%r15,gprregs-.base_crash(%r13)
0134     br  %r14
0135 
0136 .disabled_wait:
0137     lpswe   disabled_wait_psw-.base_crash(%r13)
0138 
0139 .start_crash_kernel:
0140     /* Location of purgatory_start in crash memory */
0141     larl    %r0,.base_crash
0142     larl    %r1,purgatory_start
0143     slgr    %r0,%r1
0144     lgr %r8,%r13
0145     sgr %r8,%r0
0146 
0147     /* Destination for this code i.e. end of memory to be swapped. */
0148     larl    %r0,purgatory_end
0149     larl    %r1,purgatory_start
0150     slgr    %r0,%r1
0151     lg  %r9,crash_size-.base_crash(%r13)
0152     sgr %r9,%r0
0153 
0154     /* Destination in crash memory, i.e. same as r9 but in crash memory. */
0155     lg  %r10,crash_start-.base_crash(%r13)
0156     agr %r10,%r9
0157 
0158     /* Buffer location (in crash memory) and size. As the purgatory is
0159      * behind the point of no return it can re-use the stack as buffer.
0160      */
0161     larl    %r11,purgatory_end
0162     larl    %r12,stack
0163     slgr    %r11,%r12
0164 
0165     MEMCPY  %r12,%r9,%r11   /* dst  -> (crash) buf */
0166     MEMCPY  %r9,%r8,%r11    /* self -> dst */
0167 
0168     /* Jump to new location. */
0169     lgr %r7,%r9
0170     larl    %r0,.jump_to_dst
0171     larl    %r1,purgatory_start
0172     slgr    %r0,%r1
0173     agr %r7,%r0
0174     br  %r7
0175 
0176 .jump_to_dst:
0177     basr    %r13,0
0178 .base_dst:
0179 
0180     /* clear buffer */
0181     MEMCPY  %r12,%r10,%r11  /* (crash) buf -> (crash) dst */
0182 
0183     /* Load new buffer location after jump */
0184     larl    %r7,stack
0185     lgr %r0,%r7
0186     larl    %r1,purgatory_start
0187     slgr    %r0,%r1
0188     agr %r10,%r0
0189     MEMCPY  %r10,%r7,%r11   /* (new) buf -> (crash) buf */
0190 
0191     /* Now the code is set up to run from its designated location. Start
0192      * swapping the rest of crash memory now.
0193      *
0194      * The registers will be used as follow:
0195      *
0196      *  %r0-%r4 reserved for macros defined above
0197      *  %r5-%r6 tmp registers
0198      *  %r7 pointer to current struct sha region
0199      *  %r8 index to iterate over all sha regions
0200      *  %r9 pointer in crash memory
0201      *  %r10    pointer in old kernel
0202      *  %r11    total size (still) to be moved
0203      *  %r12    pointer to buffer
0204      */
0205     lgr %r12,%r7
0206     lgr %r11,%r9
0207     lghi    %r10,0
0208     lg  %r9,crash_start-.base_dst(%r13)
0209     lghi    %r8,16  /* KEXEC_SEGMENTS_MAX */
0210     larl    %r7,purgatory_sha_regions
0211 
0212     j .loop_first
0213 
0214     /* Loop over all purgatory_sha_regions. */
0215 .loop_next:
0216     aghi    %r8,-1
0217     cghi    %r8,0
0218     je  .loop_out
0219 
0220     aghi    %r7,__KEXEC_SHA_REGION_SIZE
0221 
0222 .loop_first:
0223     lg  %r5,__KEXEC_SHA_REGION_START(%r7)
0224     cghi    %r5,0
0225     je  .loop_next
0226 
0227     /* Copy [end last sha region, start current sha region) */
0228     /* Note: kexec_sha_region->start points in crash memory */
0229     sgr %r5,%r9
0230     MEMCPY  %r9,%r10,%r5
0231 
0232     agr %r9,%r5
0233     agr %r10,%r5
0234     sgr %r11,%r5
0235 
0236     /* Swap sha region */
0237     lg  %r6,__KEXEC_SHA_REGION_LEN(%r7)
0238     MEMSWAP %r9,%r10,%r12,%r6
0239     sg  %r11,__KEXEC_SHA_REGION_LEN(%r7)
0240     j   .loop_next
0241 
0242 .loop_out:
0243     /* Copy rest of crash memory */
0244     MEMCPY  %r9,%r10,%r11
0245 
0246     /* start crash kernel */
0247     START_NEXT_KERNEL .base_dst 1
0248 
0249 
0250 load_psw_mask:
0251     .long   0x00080000,0x80000000
0252 
0253     .align  8
0254 disabled_wait_psw:
0255     .quad   0x0002000180000000
0256     .quad   0x0000000000000000 + .do_checksum_verification
0257 
0258 gprregs:
0259     .rept   10
0260     .quad   0
0261     .endr
0262 
0263 /* Macro to define a global variable with name and size (in bytes) to be
0264  * shared with C code.
0265  *
0266  * Add the .size and .type attribute to satisfy checks on the Elf_Sym during
0267  * purgatory load.
0268  */
0269 .macro GLOBAL_VARIABLE name,size
0270 \name:
0271     .global \name
0272     .size   \name,\size
0273     .type   \name,object
0274     .skip   \size,0
0275 .endm
0276 
0277 GLOBAL_VARIABLE purgatory_sha256_digest,32
0278 GLOBAL_VARIABLE purgatory_sha_regions,16*__KEXEC_SHA_REGION_SIZE
0279 GLOBAL_VARIABLE kernel_entry,8
0280 GLOBAL_VARIABLE kernel_type,8
0281 GLOBAL_VARIABLE crash_start,8
0282 GLOBAL_VARIABLE crash_size,8
0283 
0284     .align  PAGE_SIZE
0285 stack:
0286     /* The buffer to move this code must be as big as the code. */
0287     .skip   stack-purgatory_start
0288     .align  PAGE_SIZE
0289 purgatory_end: