// SPDX-License-Identifier: GPL-2.0-only1/*2* PPC64 code to handle Linux booting another kernel.3*4* Copyright (C) 2004-2005, IBM Corp.5*6* Created by: Milton D Miller II7*/8910#include <linux/kexec.h>11#include <linux/smp.h>12#include <linux/thread_info.h>13#include <linux/init_task.h>14#include <linux/errno.h>15#include <linux/kernel.h>16#include <linux/cpu.h>17#include <linux/hardirq.h>18#include <linux/of.h>19#include <linux/libfdt.h>2021#include <asm/page.h>22#include <asm/current.h>23#include <asm/machdep.h>24#include <asm/cacheflush.h>25#include <asm/firmware.h>26#include <asm/paca.h>27#include <asm/mmu.h>28#include <asm/sections.h> /* _end */29#include <asm/setup.h>30#include <asm/smp.h>31#include <asm/hw_breakpoint.h>32#include <asm/svm.h>33#include <asm/ultravisor.h>34#include <asm/crashdump-ppc64.h>3536int machine_kexec_prepare(struct kimage *image)37{38int i;39unsigned long begin, end; /* limits of segment */40unsigned long low, high; /* limits of blocked memory range */41struct device_node *node;42const unsigned long *basep;43const unsigned int *sizep;4445/*46* Since we use the kernel fault handlers and paging code to47* handle the virtual mode, we must make sure no destination48* overlaps kernel static data or bss.49*/50for (i = 0; i < image->nr_segments; i++)51if (image->segment[i].mem < __pa(_end))52return -ETXTBSY;5354/* We also should not overwrite the tce tables */55for_each_node_by_type(node, "pci") {56basep = of_get_property(node, "linux,tce-base", NULL);57sizep = of_get_property(node, "linux,tce-size", NULL);58if (basep == NULL || sizep == NULL)59continue;6061low = *basep;62high = low + (*sizep);6364for (i = 0; i < image->nr_segments; i++) {65begin = image->segment[i].mem;66end = begin + image->segment[i].memsz;6768if ((begin < high) && (end > low)) {69of_node_put(node);70return -ETXTBSY;71}72}73}7475return 0;76}7778/* Called during kexec sequence with MMU off */79static notrace void copy_segments(unsigned long ind)80{81unsigned long entry;82unsigned long *ptr;83void *dest;84void *addr;8586/*87* We rely on kexec_load to create a lists that properly88* initializes these pointers before they are used.89* We will still crash if the list is wrong, but at least90* the compiler will be quiet.91*/92ptr = NULL;93dest = NULL;9495for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {96addr = __va(entry & PAGE_MASK);9798switch (entry & IND_FLAGS) {99case IND_DESTINATION:100dest = addr;101break;102case IND_INDIRECTION:103ptr = addr;104break;105case IND_SOURCE:106copy_page(dest, addr);107dest += PAGE_SIZE;108}109}110}111112/* Called during kexec sequence with MMU off */113notrace void kexec_copy_flush(struct kimage *image)114{115long i, nr_segments = image->nr_segments;116struct kexec_segment ranges[KEXEC_SEGMENT_MAX];117118/* save the ranges on the stack to efficiently flush the icache */119memcpy(ranges, image->segment, sizeof(ranges));120121/*122* After this call we may not use anything allocated in dynamic123* memory, including *image.124*125* Only globals and the stack are allowed.126*/127copy_segments(image->head);128129/*130* we need to clear the icache for all dest pages sometime,131* including ones that were in place on the original copy132*/133for (i = 0; i < nr_segments; i++)134flush_icache_range((unsigned long)__va(ranges[i].mem),135(unsigned long)__va(ranges[i].mem + ranges[i].memsz));136}137138#ifdef CONFIG_SMP139140static int kexec_all_irq_disabled = 0;141142static void kexec_smp_down(void *arg)143{144local_irq_disable();145hard_irq_disable();146147mb(); /* make sure our irqs are disabled before we say they are */148get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;149while(kexec_all_irq_disabled == 0)150cpu_relax();151mb(); /* make sure all irqs are disabled before this */152hw_breakpoint_disable();153/*154* Now every CPU has IRQs off, we can clear out any pending155* IPIs and be sure that no more will come in after this.156*/157if (ppc_md.kexec_cpu_down)158ppc_md.kexec_cpu_down(0, 1);159160reset_sprs();161162kexec_smp_wait();163/* NOTREACHED */164}165166static void kexec_prepare_cpus_wait(int wait_state)167{168int my_cpu, i, notified=-1;169170hw_breakpoint_disable();171my_cpu = get_cpu();172/* Make sure each CPU has at least made it to the state we need.173*174* FIXME: There is a (slim) chance of a problem if not all of the CPUs175* are correctly onlined. If somehow we start a CPU on boot with RTAS176* start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in177* time, the boot CPU will timeout. If it does eventually execute178* stuff, the secondary will start up (paca_ptrs[]->cpu_start was179* written) and get into a peculiar state.180* If the platform supports smp_ops->take_timebase(), the secondary CPU181* will probably be spinning in there. If not (i.e. pseries), the182* secondary will continue on and try to online itself/idle/etc. If it183* survives that, we need to find these184* possible-but-not-online-but-should-be CPUs and chaperone them into185* kexec_smp_wait().186*/187for_each_online_cpu(i) {188if (i == my_cpu)189continue;190191while (paca_ptrs[i]->kexec_state < wait_state) {192barrier();193if (i != notified) {194printk(KERN_INFO "kexec: waiting for cpu %d "195"(physical %d) to enter %i state\n",196i, paca_ptrs[i]->hw_cpu_id, wait_state);197notified = i;198}199}200}201mb();202}203204/*205* We need to make sure each present CPU is online. The next kernel will scan206* the device tree and assume primary threads are online and query secondary207* threads via RTAS to online them if required. If we don't online primary208* threads, they will be stuck. However, we also online secondary threads as we209* may be using 'cede offline'. In this case RTAS doesn't see the secondary210* threads as offline -- and again, these CPUs will be stuck.211*212* So, we online all CPUs that should be running, including secondary threads.213*/214static void wake_offline_cpus(void)215{216int cpu = 0;217218for_each_present_cpu(cpu) {219if (!cpu_online(cpu)) {220printk(KERN_INFO "kexec: Waking offline cpu %d.\n",221cpu);222WARN_ON(add_cpu(cpu));223}224}225}226227static void kexec_prepare_cpus(void)228{229wake_offline_cpus();230smp_call_function(kexec_smp_down, NULL, /* wait */0);231local_irq_disable();232hard_irq_disable();233234mb(); /* make sure IRQs are disabled before we say they are */235get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;236237kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);238/* we are sure every CPU has IRQs off at this point */239kexec_all_irq_disabled = 1;240241/*242* Before removing MMU mappings make sure all CPUs have entered real243* mode:244*/245kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);246247/* after we tell the others to go down */248if (ppc_md.kexec_cpu_down)249ppc_md.kexec_cpu_down(0, 0);250251put_cpu();252}253254#else /* ! SMP */255256static void kexec_prepare_cpus(void)257{258/*259* move the secondarys to us so that we can copy260* the new kernel 0-0x100 safely261*262* do this if kexec in setup.c ?263*264* We need to release the cpus if we are ever going from an265* UP to an SMP kernel.266*/267smp_release_cpus();268if (ppc_md.kexec_cpu_down)269ppc_md.kexec_cpu_down(0, 0);270local_irq_disable();271hard_irq_disable();272}273274#endif /* SMP */275276/*277* kexec thread structure and stack.278*279* We need to make sure that this is 16384-byte aligned due to the280* way process stacks are handled. It also must be statically allocated281* or allocated as part of the kimage, because everything else may be282* overwritten when we copy the kexec image. We piggyback on the283* "init_task" linker section here to statically allocate a stack.284*285* We could use a smaller stack if we don't care about anything using286* current, but that audit has not been performed.287*/288static union thread_union kexec_stack = { };289290/*291* For similar reasons to the stack above, the kexecing CPU needs to be on a292* static PACA; we switch to kexec_paca.293*/294static struct paca_struct kexec_paca;295296/* Our assembly helper, in misc_64.S */297extern void kexec_sequence(void *newstack, unsigned long start,298void *image, void *control,299void (*clear_all)(void),300bool copy_with_mmu_off) __noreturn;301302/* too late to fail here */303void default_machine_kexec(struct kimage *image)304{305bool copy_with_mmu_off;306307/* prepare control code if any */308309/*310* If the kexec boot is the normal one, need to shutdown other cpus311* into our wait loop and quiesce interrupts.312* Otherwise, in the case of crashed mode (crashing_cpu >= 0),313* stopping other CPUs and collecting their pt_regs is done before314* using debugger IPI.315*/316317if (!kdump_in_progress())318kexec_prepare_cpus();319320#ifdef CONFIG_PPC_PSERIES321/*322* This must be done after other CPUs have shut down, otherwise they323* could execute the 'scv' instruction, which is not supported with324* reloc disabled (see configure_exceptions()).325*/326if (firmware_has_feature(FW_FEATURE_SET_MODE))327pseries_disable_reloc_on_exc();328#endif329330printk("kexec: Starting switchover sequence.\n");331332/* switch to a staticly allocated stack. Based on irq stack code.333* We setup preempt_count to avoid using VMX in memcpy.334* XXX: the task struct will likely be invalid once we do the copy!335*/336current_thread_info()->flags = 0;337current_thread_info()->preempt_count = HARDIRQ_OFFSET;338339/* We need a static PACA, too; copy this CPU's PACA over and switch to340* it. Also poison per_cpu_offset and NULL lppaca to catch anyone using341* non-static data.342*/343memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));344kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;345#ifdef CONFIG_PPC_PSERIES346kexec_paca.lppaca_ptr = NULL;347#endif348349if (is_secure_guest() && !(image->preserve_context ||350image->type == KEXEC_TYPE_CRASH)) {351uv_unshare_all_pages();352printk("kexec: Unshared all shared pages.\n");353}354355paca_ptrs[kexec_paca.paca_index] = &kexec_paca;356357setup_paca(&kexec_paca);358359/*360* The lppaca should be unregistered at this point so the HV won't361* touch it. In the case of a crash, none of the lppacas are362* unregistered so there is not much we can do about it here.363*/364365/*366* On Book3S, the copy must happen with the MMU off if we are either367* using Radix page tables or we are not in an LPAR since we can368* overwrite the page tables while copying.369*370* In an LPAR, we keep the MMU on otherwise we can't access beyond371* the RMA. On BookE there is no real MMU off mode, so we have to372* keep it enabled as well (but then we have bolted TLB entries).373*/374#ifdef CONFIG_PPC_BOOK3E_64375copy_with_mmu_off = false;376#else377copy_with_mmu_off = radix_enabled() ||378!(firmware_has_feature(FW_FEATURE_LPAR) ||379firmware_has_feature(FW_FEATURE_PS3_LV1));380#endif381382/* Some things are best done in assembly. Finding globals with383* a toc is easier in C, so pass in what we can.384*/385kexec_sequence(&kexec_stack, image->start, image,386page_address(image->control_code_page),387mmu_cleanup_all, copy_with_mmu_off);388/* NOTREACHED */389}390391#ifdef CONFIG_PPC_64S_HASH_MMU392/* Values we need to export to the second kernel via the device tree. */393static __be64 htab_base;394static __be64 htab_size;395396static struct property htab_base_prop = {397.name = "linux,htab-base",398.length = sizeof(unsigned long),399.value = &htab_base,400};401402static struct property htab_size_prop = {403.name = "linux,htab-size",404.length = sizeof(unsigned long),405.value = &htab_size,406};407408static int __init export_htab_values(void)409{410struct device_node *node;411412/* On machines with no htab htab_address is NULL */413if (!htab_address)414return -ENODEV;415416node = of_find_node_by_path("/chosen");417if (!node)418return -ENODEV;419420/* remove any stale properties so ours can be found */421of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL));422of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL));423424htab_base = cpu_to_be64(__pa(htab_address));425of_add_property(node, &htab_base_prop);426htab_size = cpu_to_be64(htab_size_bytes);427of_add_property(node, &htab_size_prop);428429of_node_put(node);430return 0;431}432late_initcall(export_htab_values);433#endif /* CONFIG_PPC_64S_HASH_MMU */434435#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP)436/**437* add_node_props - Reads node properties from device node structure and add438* them to fdt.439* @fdt: Flattened device tree of the kernel440* @node_offset: offset of the node to add a property at441* @dn: device node pointer442*443* Returns 0 on success, negative errno on error.444*/445static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)446{447int ret = 0;448struct property *pp;449450if (!dn)451return -EINVAL;452453for_each_property_of_node(dn, pp) {454ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);455if (ret < 0) {456pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));457return ret;458}459}460return ret;461}462463/**464* update_cpus_node - Update cpus node of flattened device tree using of_root465* device node.466* @fdt: Flattened device tree of the kernel.467*468* Returns 0 on success, negative errno on error.469*470* Note: expecting no subnodes under /cpus/<node> with device_type == "cpu".471* If this changes, update this function to include them.472*/473int update_cpus_node(void *fdt)474{475int prev_node_offset;476const char *device_type;477const struct fdt_property *prop;478struct device_node *cpus_node, *dn;479int cpus_offset, cpus_subnode_offset, ret = 0;480481cpus_offset = fdt_path_offset(fdt, "/cpus");482if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {483pr_err("Malformed device tree: error reading /cpus node: %s\n",484fdt_strerror(cpus_offset));485return cpus_offset;486}487488prev_node_offset = cpus_offset;489/* Delete sub-nodes of /cpus node with device_type == "cpu" */490for (cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset); cpus_subnode_offset >= 0;) {491/* Ignore nodes that do not have a device_type property or device_type != "cpu" */492prop = fdt_get_property(fdt, cpus_subnode_offset, "device_type", NULL);493if (!prop || strcmp(prop->data, "cpu")) {494prev_node_offset = cpus_subnode_offset;495goto next_node;496}497498ret = fdt_del_node(fdt, cpus_subnode_offset);499if (ret < 0) {500pr_err("Failed to delete a cpus sub-node: %s\n", fdt_strerror(ret));501return ret;502}503next_node:504if (prev_node_offset == cpus_offset)505cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset);506else507cpus_subnode_offset = fdt_next_subnode(fdt, prev_node_offset);508}509510cpus_node = of_find_node_by_path("/cpus");511/* Fail here to avoid kexec/kdump kernel boot hung */512if (!cpus_node) {513pr_err("No /cpus node found\n");514return -EINVAL;515}516517/* Add all /cpus sub-nodes of device_type == "cpu" to FDT */518for_each_child_of_node(cpus_node, dn) {519/* Ignore device nodes that do not have a device_type property520* or device_type != "cpu".521*/522device_type = of_get_property(dn, "device_type", NULL);523if (!device_type || strcmp(device_type, "cpu"))524continue;525526cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);527if (cpus_subnode_offset < 0) {528pr_err("Unable to add %s subnode: %s\n", dn->full_name,529fdt_strerror(cpus_subnode_offset));530ret = cpus_subnode_offset;531goto out;532}533534ret = add_node_props(fdt, cpus_subnode_offset, dn);535if (ret < 0)536goto out;537}538out:539of_node_put(cpus_node);540of_node_put(dn);541return ret;542}543#endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */544545546