cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

file_load.c (3343B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * powerpc code to implement the kexec_file_load syscall
      4 *
      5 * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
      6 * Copyright (C) 2004  IBM Corp.
      7 * Copyright (C) 2004,2005  Milton D Miller II, IBM Corporation
      8 * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
      9 * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
     10 * Copyright (C) 2016  IBM Corporation
     11 *
     12 * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
     13 * Heavily modified for the kernel by
     14 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
     15 */
     16
     17#include <linux/slab.h>
     18#include <linux/kexec.h>
     19#include <linux/of_fdt.h>
     20#include <linux/libfdt.h>
     21#include <asm/setup.h>
     22
     23#define SLAVE_CODE_SIZE		256	/* First 0x100 bytes */
     24
     25/**
     26 * setup_kdump_cmdline - Prepend "elfcorehdr=<addr> " to command line
     27 *                       of kdump kernel for exporting the core.
     28 * @image:               Kexec image
     29 * @cmdline:             Command line parameters to update.
     30 * @cmdline_len:         Length of the cmdline parameters.
     31 *
     32 * kdump segment must be setup before calling this function.
     33 *
     34 * Returns new cmdline buffer for kdump kernel on success, NULL otherwise.
     35 */
     36char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
     37			  unsigned long cmdline_len)
     38{
     39	int elfcorehdr_strlen;
     40	char *cmdline_ptr;
     41
     42	cmdline_ptr = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
     43	if (!cmdline_ptr)
     44		return NULL;
     45
     46	elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
     47				    image->elf_load_addr);
     48
     49	if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
     50		pr_err("Appending elfcorehdr=<addr> exceeds cmdline size\n");
     51		kfree(cmdline_ptr);
     52		return NULL;
     53	}
     54
     55	memcpy(cmdline_ptr + elfcorehdr_strlen, cmdline, cmdline_len);
     56	// Ensure it's nul terminated
     57	cmdline_ptr[COMMAND_LINE_SIZE - 1] = '\0';
     58	return cmdline_ptr;
     59}
     60
     61/**
     62 * setup_purgatory - initialize the purgatory's global variables
     63 * @image:		kexec image.
     64 * @slave_code:		Slave code for the purgatory.
     65 * @fdt:		Flattened device tree for the next kernel.
     66 * @kernel_load_addr:	Address where the kernel is loaded.
     67 * @fdt_load_addr:	Address where the flattened device tree is loaded.
     68 *
     69 * Return: 0 on success, or negative errno on error.
     70 */
     71int setup_purgatory(struct kimage *image, const void *slave_code,
     72		    const void *fdt, unsigned long kernel_load_addr,
     73		    unsigned long fdt_load_addr)
     74{
     75	unsigned int *slave_code_buf, master_entry;
     76	int ret;
     77
     78	slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
     79	if (!slave_code_buf)
     80		return -ENOMEM;
     81
     82	/* Get the slave code from the new kernel and put it in purgatory. */
     83	ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
     84					     slave_code_buf, SLAVE_CODE_SIZE,
     85					     true);
     86	if (ret) {
     87		kfree(slave_code_buf);
     88		return ret;
     89	}
     90
     91	master_entry = slave_code_buf[0];
     92	memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
     93	slave_code_buf[0] = master_entry;
     94	ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
     95					     slave_code_buf, SLAVE_CODE_SIZE,
     96					     false);
     97	kfree(slave_code_buf);
     98
     99	ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
    100					     sizeof(kernel_load_addr), false);
    101	if (ret)
    102		return ret;
    103	ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
    104					     sizeof(fdt_load_addr), false);
    105	if (ret)
    106		return ret;
    107
    108	return 0;
    109}