Path: blob/master/arch/tile/kernel/machine_kexec.c
10817 views
/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*13* based on machine_kexec.c from other architectures in linux-2.6.1814*/1516#include <linux/mm.h>17#include <linux/kexec.h>18#include <linux/delay.h>19#include <linux/reboot.h>20#include <linux/errno.h>21#include <linux/vmalloc.h>22#include <linux/cpumask.h>23#include <linux/kernel.h>24#include <linux/elf.h>25#include <linux/highmem.h>26#include <linux/mmu_context.h>27#include <linux/io.h>28#include <linux/timex.h>29#include <asm/pgtable.h>30#include <asm/pgalloc.h>31#include <asm/cacheflush.h>32#include <asm/checksum.h>33#include <hv/hypervisor.h>343536/*37* This stuff is not in elf.h and is not in any other kernel include.38* This stuff is needed below in the little boot notes parser to39* extract the command line so we can pass it to the hypervisor.40*/41struct Elf32_Bhdr {42Elf32_Word b_signature;43Elf32_Word b_size;44Elf32_Half b_checksum;45Elf32_Half b_records;46};47#define ELF_BOOT_MAGIC 0x0E1FB00748#define EBN_COMMAND_LINE 0x0000000449#define roundupsz(X) (((X) + 3) & ~3)5051/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */525354void machine_shutdown(void)55{56/*57* Normally we would stop all the other processors here, but58* the check in machine_kexec_prepare below ensures we'll only59* get this far if we've been booted with "nosmp" on the60* command line or without CONFIG_SMP so there's nothing to do61* here (for now).62*/63}6465void machine_crash_shutdown(struct pt_regs *regs)66{67/*68* Cannot happen. This type of kexec is disabled on this69* architecture (and enforced in machine_kexec_prepare below).70*/71}727374int machine_kexec_prepare(struct kimage *image)75{76if (num_online_cpus() > 1) {77pr_warning("%s: detected attempt to kexec "78"with num_online_cpus() > 1\n",79__func__);80return -ENOSYS;81}82if (image->type != KEXEC_TYPE_DEFAULT) {83pr_warning("%s: detected attempt to kexec "84"with unsupported type: %d\n",85__func__,86image->type);87return -ENOSYS;88}89return 0;90}9192void machine_kexec_cleanup(struct kimage *image)93{94/*95* We did nothing in machine_kexec_prepare,96* so we have nothing to do here.97*/98}99100/*101* If we can find elf boot notes on this page, return the command102* line. Otherwise, silently return null. Somewhat kludgy, but no103* good way to do this without significantly rearchitecting the104* architecture-independent kexec code.105*/106107static unsigned char *kexec_bn2cl(void *pg)108{109struct Elf32_Bhdr *bhdrp;110Elf32_Nhdr *nhdrp;111unsigned char *desc;112unsigned char *command_line;113__sum16 csum;114115bhdrp = (struct Elf32_Bhdr *) pg;116117/*118* This routine is invoked for every source page, so make119* sure to quietly ignore every impossible page.120*/121if (bhdrp->b_signature != ELF_BOOT_MAGIC ||122bhdrp->b_size > PAGE_SIZE)123return 0;124125/*126* If we get a checksum mismatch, warn with the checksum127* so we can diagnose better.128*/129csum = ip_compute_csum(pg, bhdrp->b_size);130if (csum != 0) {131pr_warning("%s: bad checksum %#x (size %d)\n",132__func__, csum, bhdrp->b_size);133return 0;134}135136nhdrp = (Elf32_Nhdr *) (bhdrp + 1);137138while (nhdrp->n_type != EBN_COMMAND_LINE) {139140desc = (unsigned char *) (nhdrp + 1);141desc += roundupsz(nhdrp->n_descsz);142143nhdrp = (Elf32_Nhdr *) desc;144145/* still in bounds? */146if ((unsigned char *) (nhdrp + 1) >147((unsigned char *) pg) + bhdrp->b_size) {148149pr_info("%s: out of bounds\n", __func__);150return 0;151}152}153154command_line = (unsigned char *) (nhdrp + 1);155desc = command_line;156157while (*desc != '\0') {158desc++;159if (((unsigned long)desc & PAGE_MASK) != (unsigned long)pg) {160pr_info("%s: ran off end of page\n",161__func__);162return 0;163}164}165166return command_line;167}168169static void kexec_find_and_set_command_line(struct kimage *image)170{171kimage_entry_t *ptr, entry;172173unsigned char *command_line = 0;174unsigned char *r;175HV_Errno hverr;176177for (ptr = &image->head;178(entry = *ptr) && !(entry & IND_DONE);179ptr = (entry & IND_INDIRECTION) ?180phys_to_virt((entry & PAGE_MASK)) : ptr + 1) {181182if ((entry & IND_SOURCE)) {183void *va =184kmap_atomic_pfn(entry >> PAGE_SHIFT);185r = kexec_bn2cl(va);186if (r) {187command_line = r;188break;189}190kunmap_atomic(va);191}192}193194if (command_line != 0) {195pr_info("setting new command line to \"%s\"\n",196command_line);197198hverr = hv_set_command_line(199(HV_VirtAddr) command_line, strlen(command_line));200kunmap_atomic(command_line);201} else {202pr_info("%s: no command line found; making empty\n",203__func__);204hverr = hv_set_command_line((HV_VirtAddr) command_line, 0);205}206if (hverr)207pr_warning("%s: hv_set_command_line returned error: %d\n",208__func__, hverr);209}210211/*212* The kexec code range-checks all its PAs, so to avoid having it run213* amok and allocate memory and then sequester it from every other214* controller, we force it to come from controller zero. We also215* disable the oom-killer since if we do end up running out of memory,216* that almost certainly won't help.217*/218struct page *kimage_alloc_pages_arch(gfp_t gfp_mask, unsigned int order)219{220gfp_mask |= __GFP_THISNODE | __GFP_NORETRY;221return alloc_pages_node(0, gfp_mask, order);222}223224static void setup_quasi_va_is_pa(void)225{226HV_PTE *pgtable;227HV_PTE pte;228int i;229230/*231* Flush our TLB to prevent conflicts between the previous contents232* and the new stuff we're about to add.233*/234local_flush_tlb_all();235236/* setup VA is PA, at least up to PAGE_OFFSET */237238pgtable = (HV_PTE *)current->mm->pgd;239pte = hv_pte(_PAGE_KERNEL | _PAGE_HUGE_PAGE);240pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);241242for (i = 0; i < pgd_index(PAGE_OFFSET); i++) {243unsigned long pfn = i << (HPAGE_SHIFT - PAGE_SHIFT);244if (pfn_valid(pfn))245__set_pte(&pgtable[i], pfn_pte(pfn, pte));246}247}248249250NORET_TYPE void machine_kexec(struct kimage *image)251{252void *reboot_code_buffer;253NORET_TYPE void (*rnk)(unsigned long, void *, unsigned long)254ATTRIB_NORET;255256/* Mask all interrupts before starting to reboot. */257interrupt_mask_set_mask(~0ULL);258259kexec_find_and_set_command_line(image);260261/*262* Adjust the home caching of the control page to be cached on263* this cpu, and copy the assembly helper into the control264* code page, which we map in the vmalloc area.265*/266homecache_change_page_home(image->control_code_page, 0,267smp_processor_id());268reboot_code_buffer = vmap(&image->control_code_page, 1, 0,269__pgprot(_PAGE_KERNEL | _PAGE_EXECUTABLE));270memcpy(reboot_code_buffer, relocate_new_kernel,271relocate_new_kernel_size);272__flush_icache_range(273(unsigned long) reboot_code_buffer,274(unsigned long) reboot_code_buffer + relocate_new_kernel_size);275276setup_quasi_va_is_pa();277278/* now call it */279rnk = reboot_code_buffer;280(*rnk)(image->head, reboot_code_buffer, image->start);281}282283284