machine_kexec_64.c 11.2 KB
Newer Older
1
/*
2
 * PPC64 code to handle Linux booting another kernel.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Copyright (C) 2004-2005, IBM Corp.
 *
 * Created by: Milton D Miller II
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2.  See the file COPYING for more details.
 */


#include <linux/kexec.h>
#include <linux/smp.h>
#include <linux/thread_info.h>
16
#include <linux/init_task.h>
17
#include <linux/errno.h>
18
#include <linux/kernel.h>
19
#include <linux/cpu.h>
20
#include <linux/hardirq.h>
21 22 23 24 25 26 27 28 29

#include <asm/page.h>
#include <asm/current.h>
#include <asm/machdep.h>
#include <asm/cacheflush.h>
#include <asm/paca.h>
#include <asm/mmu.h>
#include <asm/sections.h>	/* _end */
#include <asm/prom.h>
30
#include <asm/smp.h>
31
#include <asm/hw_breakpoint.h>
32

33
int default_machine_kexec_prepare(struct kimage *image)
34 35 36 37 38
{
	int i;
	unsigned long begin, end;	/* limits of segment */
	unsigned long low, high;	/* limits of blocked memory range */
	struct device_node *node;
39 40
	const unsigned long *basep;
	const unsigned int *sizep;
41 42 43 44 45 46 47 48 49

	if (!ppc_md.hpte_clear_all)
		return -ENOENT;

	/*
	 * Since we use the kernel fault handlers and paging code to
	 * handle the virtual mode, we must make sure no destination
	 * overlaps kernel static data or bss.
	 */
Maneesh Soni's avatar
Maneesh Soni committed
50
	for (i = 0; i < image->nr_segments; i++)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		if (image->segment[i].mem < __pa(_end))
			return -ETXTBSY;

	/*
	 * For non-LPAR, we absolutely can not overwrite the mmu hash
	 * table, since we are still using the bolted entries in it to
	 * do the copy.  Check that here.
	 *
	 * It is safe if the end is below the start of the blocked
	 * region (end <= low), or if the beginning is after the
	 * end of the blocked region (begin >= high).  Use the
	 * boolean identity !(a || b)  === (!a && !b).
	 */
	if (htab_address) {
		low = __pa(htab_address);
66
		high = low + htab_size_bytes;
67

Maneesh Soni's avatar
Maneesh Soni committed
68
		for (i = 0; i < image->nr_segments; i++) {
69 70 71 72 73 74 75 76 77
			begin = image->segment[i].mem;
			end = begin + image->segment[i].memsz;

			if ((begin < high) && (end > low))
				return -ETXTBSY;
		}
	}

	/* We also should not overwrite the tce tables */
78
	for_each_node_by_type(node, "pci") {
79 80
		basep = of_get_property(node, "linux,tce-base", NULL);
		sizep = of_get_property(node, "linux,tce-size", NULL);
81 82 83 84 85 86
		if (basep == NULL || sizep == NULL)
			continue;

		low = *basep;
		high = low + (*sizep);

Maneesh Soni's avatar
Maneesh Soni committed
87
		for (i = 0; i < image->nr_segments; i++) {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
			begin = image->segment[i].mem;
			end = begin + image->segment[i].memsz;

			if ((begin < high) && (end > low))
				return -ETXTBSY;
		}
	}

	return 0;
}

#define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)

static void copy_segments(unsigned long ind)
{
	unsigned long entry;
	unsigned long *ptr;
	void *dest;
	void *addr;

	/*
	 * We rely on kexec_load to create a lists that properly
	 * initializes these pointers before they are used.
	 * We will still crash if the list is wrong, but at least
	 * the compiler will be quiet.
	 */
	ptr = NULL;
	dest = NULL;

	for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
		addr = __va(entry & PAGE_MASK);

		switch (entry & IND_FLAGS) {
		case IND_DESTINATION:
			dest = addr;
			break;
		case IND_INDIRECTION:
			ptr = addr;
			break;
		case IND_SOURCE:
			copy_page(dest, addr);
			dest += PAGE_SIZE;
		}
	}
}

void kexec_copy_flush(struct kimage *image)
{
	long i, nr_segments = image->nr_segments;
	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];

	/* save the ranges on the stack to efficiently flush the icache */
	memcpy(ranges, image->segment, sizeof(ranges));

	/*
	 * After this call we may not use anything allocated in dynamic
	 * memory, including *image.
	 *
	 * Only globals and the stack are allowed.
	 */
	copy_segments(image->head);

	/*
	 * we need to clear the icache for all dest pages sometime,
	 * including ones that were in place on the original copy
	 */
	for (i = 0; i < nr_segments; i++)
155 156
		flush_icache_range((unsigned long)__va(ranges[i].mem),
			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
157 158 159 160
}

#ifdef CONFIG_SMP

161 162
static int kexec_all_irq_disabled = 0;

163
static void kexec_smp_down(void *arg)
164
{
165
	local_irq_disable();
166 167
	hard_irq_disable();

168 169 170 171 172
	mb(); /* make sure our irqs are disabled before we say they are */
	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
	while(kexec_all_irq_disabled == 0)
		cpu_relax();
	mb(); /* make sure all irqs are disabled before this */
173
	hw_breakpoint_disable();
174 175 176 177
	/*
	 * Now every CPU has IRQs off, we can clear out any pending
	 * IPIs and be sure that no more will come in after this.
	 */
178 179
	if (ppc_md.kexec_cpu_down)
		ppc_md.kexec_cpu_down(0, 1);
180 181 182 183 184

	kexec_smp_wait();
	/* NOTREACHED */
}

185
static void kexec_prepare_cpus_wait(int wait_state)
186 187 188
{
	int my_cpu, i, notified=-1;

189
	hw_breakpoint_disable();
190
	my_cpu = get_cpu();
191 192 193 194 195 196 197 198 199 200 201 202 203 204
	/* Make sure each CPU has at least made it to the state we need.
	 *
	 * FIXME: There is a (slim) chance of a problem if not all of the CPUs
	 * are correctly onlined.  If somehow we start a CPU on boot with RTAS
	 * start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in
	 * time, the boot CPU will timeout.  If it does eventually execute
	 * stuff, the secondary will start up (paca[].cpu_start was written) and
	 * get into a peculiar state.  If the platform supports
	 * smp_ops->take_timebase(), the secondary CPU will probably be spinning
	 * in there.  If not (i.e. pseries), the secondary will continue on and
	 * try to online itself/idle/etc. If it survives that, we need to find
	 * these possible-but-not-online-but-should-be CPUs and chaperone them
	 * into kexec_smp_wait().
	 */
205
	for_each_online_cpu(i) {
206 207 208
		if (i == my_cpu)
			continue;

209
		while (paca[i].kexec_state < wait_state) {
210
			barrier();
211
			if (i != notified) {
212 213 214
				printk(KERN_INFO "kexec: waiting for cpu %d "
				       "(physical %d) to enter %i state\n",
				       i, paca[i].hw_cpu_id, wait_state);
215 216 217 218
				notified = i;
			}
		}
	}
219 220 221
	mb();
}

222 223 224 225 226 227 228 229 230 231 232
/*
 * We need to make sure each present CPU is online.  The next kernel will scan
 * the device tree and assume primary threads are online and query secondary
 * threads via RTAS to online them if required.  If we don't online primary
 * threads, they will be stuck.  However, we also online secondary threads as we
 * may be using 'cede offline'.  In this case RTAS doesn't see the secondary
 * threads as offline -- and again, these CPUs will be stuck.
 *
 * So, we online all CPUs that should be running, including secondary threads.
 */
static void wake_offline_cpus(void)
233
{
234 235 236 237 238 239
	int cpu = 0;

	for_each_present_cpu(cpu) {
		if (!cpu_online(cpu)) {
			printk(KERN_INFO "kexec: Waking offline cpu %d.\n",
			       cpu);
240
			WARN_ON(cpu_up(cpu));
241 242 243
		}
	}
}
244

245 246 247
static void kexec_prepare_cpus(void)
{
	wake_offline_cpus();
248 249
	smp_call_function(kexec_smp_down, NULL, /* wait */0);
	local_irq_disable();
250 251
	hard_irq_disable();

252 253 254 255 256 257
	mb(); /* make sure IRQs are disabled before we say they are */
	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;

	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
	/* we are sure every CPU has IRQs off at this point */
	kexec_all_irq_disabled = 1;
258 259

	/* after we tell the others to go down */
260 261
	if (ppc_md.kexec_cpu_down)
		ppc_md.kexec_cpu_down(0, 0);
262

263 264 265 266
	/*
	 * Before removing MMU mappings make sure all CPUs have entered real
	 * mode:
	 */
267
	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
268

269
	put_cpu();
270 271 272 273 274 275 276 277 278 279 280
}

#else /* ! SMP */

static void kexec_prepare_cpus(void)
{
	/*
	 * move the secondarys to us so that we can copy
	 * the new kernel 0-0x100 safely
	 *
	 * do this if kexec in setup.c ?
281 282 283
	 *
	 * We need to release the cpus if we are ever going from an
	 * UP to an SMP kernel.
284
	 */
285
	smp_release_cpus();
286 287
	if (ppc_md.kexec_cpu_down)
		ppc_md.kexec_cpu_down(0, 0);
288
	local_irq_disable();
289
	hard_irq_disable();
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

#endif /* SMP */

/*
 * kexec thread structure and stack.
 *
 * We need to make sure that this is 16384-byte aligned due to the
 * way process stacks are handled.  It also must be statically allocated
 * or allocated as part of the kimage, because everything else may be
 * overwritten when we copy the kexec image.  We piggyback on the
 * "init_task" linker section here to statically allocate a stack.
 *
 * We could use a smaller stack if we don't care about anything using
 * current, but that audit has not been performed.
 */
306 307
static union thread_union kexec_stack __init_task_data =
	{ };
308

309 310 311 312 313 314
/*
 * For similar reasons to the stack above, the kexecing CPU needs to be on a
 * static PACA; we switch to kexec_paca.
 */
struct paca_struct kexec_paca;

315
/* Our assembly helper, in misc_64.S */
316 317
extern void kexec_sequence(void *newstack, unsigned long start,
			   void *image, void *control,
318
			   void (*clear_all)(void)) __noreturn;
319 320

/* too late to fail here */
321
void default_machine_kexec(struct kimage *image)
322 323 324
{
	/* prepare control code if any */

325 326 327 328 329 330 331 332
	/*
        * If the kexec boot is the normal one, need to shutdown other cpus
        * into our wait loop and quiesce interrupts.
        * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
        * stopping other CPUs and collecting their pt_regs is done before
        * using debugger IPI.
        */

333
	if (!kdump_in_progress())
334
		kexec_prepare_cpus();
335

336 337
	pr_debug("kexec: Starting switchover sequence.\n");

338
	/* switch to a staticly allocated stack.  Based on irq stack code.
339
	 * We setup preempt_count to avoid using VMX in memcpy.
340 341 342 343
	 * XXX: the task struct will likely be invalid once we do the copy!
	 */
	kexec_stack.thread_info.task = current_thread_info()->task;
	kexec_stack.thread_info.flags = 0;
344 345
	kexec_stack.thread_info.preempt_count = HARDIRQ_OFFSET;
	kexec_stack.thread_info.cpu = current_thread_info()->cpu;
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360
	/* We need a static PACA, too; copy this CPU's PACA over and switch to
	 * it.  Also poison per_cpu_offset to catch anyone using non-static
	 * data.
	 */
	memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));
	kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;
	paca = (struct paca_struct *)RELOC_HIDE(&kexec_paca, 0) -
		kexec_paca.paca_index;
	setup_paca(&kexec_paca);

	/* XXX: If anyone does 'dynamic lppacas' this will also need to be
	 * switched to a static version!
	 */

361 362 363 364 365
	/* Some things are best done in assembly.  Finding globals with
	 * a toc is easier in C, so pass in what we can.
	 */
	kexec_sequence(&kexec_stack, image->start, image,
			page_address(image->control_code_page),
366
			ppc_md.hpte_clear_all);
367 368
	/* NOTREACHED */
}
369 370

/* Values we need to export to the second kernel via the device tree. */
371
static unsigned long htab_base;
372
static unsigned long htab_size;
373 374 375 376

static struct property htab_base_prop = {
	.name = "linux,htab-base",
	.length = sizeof(unsigned long),
377
	.value = &htab_base,
378 379 380 381 382
};

static struct property htab_size_prop = {
	.name = "linux,htab-size",
	.length = sizeof(unsigned long),
383
	.value = &htab_size,
384 385
};

386
static int __init export_htab_values(void)
387 388
{
	struct device_node *node;
389
	struct property *prop;
390

391 392
	/* On machines with no htab htab_address is NULL */
	if (!htab_address)
393
		return -ENODEV;
394

395 396
	node = of_find_node_by_path("/chosen");
	if (!node)
397
		return -ENODEV;
398

399 400 401
	/* remove any stale propertys so ours can be found */
	prop = of_find_property(node, htab_base_prop.name, NULL);
	if (prop)
402
		of_remove_property(node, prop);
403 404
	prop = of_find_property(node, htab_size_prop.name, NULL);
	if (prop)
405
		of_remove_property(node, prop);
406

407
	htab_base = cpu_to_be64(__pa(htab_address));
408
	of_add_property(node, &htab_base_prop);
409
	htab_size = cpu_to_be64(htab_size_bytes);
410
	of_add_property(node, &htab_size_prop);
411 412

	of_node_put(node);
413
	return 0;
414
}
415
late_initcall(export_htab_values);