]> git.vomp.tv Git - makedevenv.git/blob - kernel/patches-2.4.31/kexec-ppc-2.4.patch
Initial import
[makedevenv.git] / kernel / patches-2.4.31 / kexec-ppc-2.4.patch
1 diff -Narup linux-2.4.31-orig/arch/ppc/config.in linux-2.4.31/arch/ppc/config.in
2 --- linux-2.4.31-orig/arch/ppc/config.in        2004-08-07 16:26:04.000000000 -0700
3 +++ linux-2.4.31/arch/ppc/config.in     2005-08-24 22:14:47.000000000 -0700
4 @@ -167,6 +167,8 @@ if [ "$CONFIG_SMP" = "y" ]; then
5    int  'Maximum number of CPUs (2-32)' CONFIG_NR_CPUS 32
6  fi
7  
8 +bool 'kexec system call' CONFIG_KEXEC
9 +
10  if [ "$CONFIG_6xx" = "y" -a "$CONFIG_8260" = "n" ];then
11    bool 'AltiVec Support' CONFIG_ALTIVEC
12    bool 'Thermal Management Support' CONFIG_TAU
13 diff -Narup linux-2.4.31-orig/arch/ppc/kernel/Makefile linux-2.4.31/arch/ppc/kernel/Makefile
14 --- linux-2.4.31-orig/arch/ppc/kernel/Makefile  2004-04-14 06:05:27.000000000 -0700
15 +++ linux-2.4.31/arch/ppc/kernel/Makefile       2005-08-24 22:17:32.000000000 -0700
16 @@ -49,6 +49,7 @@ obj-$(CONFIG_PCI)             += pci-dma.o
17  obj-$(CONFIG_KGDB)             += ppc-stub.o
18  obj-$(CONFIG_PPCBUG_NVRAM)     += prep_nvram.o
19  obj-$(CONFIG_SMP)              += smp.o
20 +obj-$(CONFIG_KEXEC)            += machine_kexec.o relocate_kernel.o
21  obj-$(CONFIG_TAU)              += temp.o
22  ifeq ($(CONFIG_SERIAL)$(CONFIG_GEN550),yy)
23  obj-$(CONFIG_KGDB)             += gen550_kgdb.o gen550_dbg.o
24 diff -Narup linux-2.4.31-orig/arch/ppc/kernel/machine_kexec.c linux-2.4.31/arch/ppc/kernel/machine_kexec.c
25 --- linux-2.4.31-orig/arch/ppc/kernel/machine_kexec.c   1969-12-31 16:00:00.000000000 -0800
26 +++ linux-2.4.31/arch/ppc/kernel/machine_kexec.c        2005-08-24 23:43:17.000000000 -0700
27 @@ -0,0 +1,121 @@
28 +/*
29 + * machine_kexec.c - handle transition of Linux booting another kernel
30 + * Copyright (C) 2002-2003 Eric Biederman  <ebiederm@xmission.com>
31 + *
32 + * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
33 + *
34 + * This source code is licensed under the GNU General Public License,
35 + * Version 2.  See the file COPYING for more details.
36 + */
37 +
38 +#include <linux/mm.h>
39 +#include <linux/kexec.h>
40 +#include <linux/delay.h>
41 +#include <linux/reboot.h>
42 +#include <asm/pgtable.h>
43 +#include <asm/pgalloc.h>
44 +#include <asm/mmu_context.h>
45 +#include <asm/io.h>
46 +#include <asm/hw_irq.h>
47 +//#include <asm/cacheflush.h>
48 +#include <asm/machdep.h>
49 +
50 +typedef NORET_TYPE void (*relocate_new_kernel_t)(
51 +       unsigned long indirection_page, unsigned long reboot_code_buffer,
52 +       unsigned long start_address) ATTRIB_NORET;
53 +
54 +const extern unsigned char relocate_new_kernel[];
55 +const extern unsigned int relocate_new_kernel_size;
56 +
57 +void machine_shutdown(void)
58 +{
59 +       if (ppc_md.machine_shutdown) {
60 +               ppc_md.machine_shutdown();
61 +       }
62 +}
63 +
64 +void machine_crash_shutdown(void)
65 +{
66 +       if (ppc_md.machine_crash_shutdown) {
67 +               ppc_md.machine_crash_shutdown();
68 +       }
69 +}
70 +
71 +/*
72 + * Do what every setup is needed on image and the
73 + * reboot code buffer to allow us to avoid allocations
74 + * later.
75 + */
76 +int machine_kexec_prepare(struct kimage *image)
77 +{
78 +       if (ppc_md.machine_kexec_prepare) {
79 +               return ppc_md.machine_kexec_prepare(image);
80 +       }
81 +       /*
82 +        * Fail if platform doesn't provide its own machine_kexec_prepare
83 +        * implementation.
84 +        */
85 +       return -ENOSYS;
86 +}
87 +
88 +void machine_kexec_cleanup(struct kimage *image)
89 +{
90 +       if (ppc_md.machine_kexec_cleanup) {
91 +               ppc_md.machine_kexec_cleanup(image);
92 +       }
93 +}
94 +
95 +/*
96 + * Do not allocate memory (or fail in any way) in machine_kexec().
97 + * We are past the point of no return, committed to rebooting now.
98 + */
99 +NORET_TYPE void machine_kexec(struct kimage *image)
100 +{
101 +       if (ppc_md.machine_kexec) {
102 +               ppc_md.machine_kexec(image);
103 +       } else {
104 +               /*
105 +                * Fall back to normal restart if platform doesn't provide
106 +                * its own kexec function, and user insist to kexec...
107 +                */
108 +               machine_restart(NULL);
109 +       }
110 +       for(;;);
111 +}
112 +
113 +
114 +/*
115 + * This is a generic machine_kexec function suitable at least for
116 + * non-OpenFirmware embedded platforms.
117 + * It merely copies the image relocation code to the control page and
118 + * jumps to it.
119 + * A platform specific function may just call this one.
120 + */
121 +void machine_kexec_simple(struct kimage *image)
122 +{
123 +       unsigned long page_list;
124 +       unsigned long reboot_code_buffer, reboot_code_buffer_phys;
125 +       relocate_new_kernel_t rnk;
126 +
127 +       /* Interrupts aren't acceptable while we reboot */
128 +       local_irq_disable();
129 +
130 +       page_list = image->head;
131 +
132 +       /* we need both effective and real address here */
133 +       reboot_code_buffer =
134 +               (unsigned long)page_address(image->control_code_page);
135 +       reboot_code_buffer_phys = virt_to_phys((void *)reboot_code_buffer);
136 +
137 +       /* copy our kernel relocation code to the control code page */
138 +       memcpy((void *)reboot_code_buffer,
139 +               relocate_new_kernel, relocate_new_kernel_size);
140 +
141 +       flush_icache_range(reboot_code_buffer,
142 +               reboot_code_buffer + KEXEC_CONTROL_CODE_SIZE);
143 +       printk(KERN_INFO "Bye!\n");
144 +
145 +       /* now call it */
146 +       rnk = (relocate_new_kernel_t) reboot_code_buffer;
147 +       (*rnk)(page_list, reboot_code_buffer_phys, image->start);
148 +}
149 diff -Narup linux-2.4.31-orig/arch/ppc/kernel/misc.S linux-2.4.31/arch/ppc/kernel/misc.S
150 --- linux-2.4.31-orig/arch/ppc/kernel/misc.S    2004-04-14 06:05:27.000000000 -0700
151 +++ linux-2.4.31/arch/ppc/kernel/misc.S 2005-08-24 22:22:21.000000000 -0700
152 @@ -1310,6 +1310,25 @@ _GLOBAL(sys_call_table)
153         .long sys_ni_syscall    /*      reserved for sys_clock_getres */
154         .long sys_ni_syscall    /*      reserved for sys_clock_nanosleep */
155         .long sys_swapcontext
156 +       .long sys_ni_syscall    /* 250 */
157 +       .long sys_ni_syscall
158 +       .long sys_ni_syscall
159 +       .long sys_ni_syscall
160 +       .long sys_ni_syscall
161 +       .long sys_ni_syscall    /* 255 */
162 +       .long sys_ni_syscall
163 +       .long sys_ni_syscall
164 +       .long sys_ni_syscall
165 +       .long sys_ni_syscall
166 +       .long sys_ni_syscall    /* 260 */
167 +       .long sys_ni_syscall
168 +       .long sys_ni_syscall
169 +       .long sys_ni_syscall
170 +       .long sys_ni_syscall
171 +       .long sys_ni_syscall    /* 265 */
172 +       .long sys_ni_syscall
173 +       .long sys_ni_syscall
174 +       .long sys_kexec_load    /* and finally, 268 sys_kexec_load... */
175  
176         .rept NR_syscalls-(.-sys_call_table)/4
177                 .long sys_ni_syscall
178 diff -Narup linux-2.4.31-orig/arch/ppc/kernel/relocate_kernel.S linux-2.4.31/arch/ppc/kernel/relocate_kernel.S
179 --- linux-2.4.31-orig/arch/ppc/kernel/relocate_kernel.S 1969-12-31 16:00:00.000000000 -0800
180 +++ linux-2.4.31/arch/ppc/kernel/relocate_kernel.S      2005-08-24 11:37:15.000000000 -0700
181 @@ -0,0 +1,122 @@
182 +/*
183 + * relocate_kernel.S - put the kernel image in place to boot
184 + * Copyright (C) 2002-2003 Eric Biederman  <ebiederm@xmission.com>
185 + *
186 + * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
187 + *
188 + * This source code is licensed under the GNU General Public License,
189 + * Version 2.  See the file COPYING for more details.
190 + */
191 +
192 +#include <asm/ppc_asm.h>
193 +#include <asm/processor.h>
194 +
195 +#include <asm/kexec.h>
196 +
197 +#define PAGE_SIZE      4096 /* must be same value as in <asm/page.h> */
198 +
199 +       /*
200 +        * Must be relocatable PIC code callable as a C function.
201 +        */
202 +       .globl relocate_new_kernel
203 +relocate_new_kernel:
204 +       /* r3 = page_list   */
205 +       /* r4 = reboot_code_buffer */
206 +       /* r5 = start_address      */
207 +
208 +       li      r0, 0
209 +
210 +       /*
211 +        * Set Machine Status Register to a known status,
212 +        * switch the MMU off and jump to 1: in a single step.
213 +        */
214 +
215 +       mr      r8, r0
216 +       ori     r8, r8, MSR_RI|MSR_ME
217 +       mtspr   SPRN_SRR1, r8
218 +       addi    r8, r4, 1f - relocate_new_kernel
219 +       mtspr   SPRN_SRR0, r8
220 +       sync
221 +       rfi
222 +
223 +1:
224 +       /* from this point address translation is turned off */
225 +       /* and interrupts are disabled */
226 +
227 +       /* set a new stack at the bottom of our page... */
228 +       /* (not really needed now) */
229 +       addi    r1, r4, KEXEC_CONTROL_CODE_SIZE - 8 /* for LR Save+Back Chain */
230 +       stw     r0, 0(r1)
231 +
232 +       /* Do the copies */
233 +       li      r6, 0 /* checksum */
234 +       mr      r0, r3
235 +       b       1f
236 +
237 +0:     /* top, read another word for the indirection page */
238 +       lwzu    r0, 4(r3)
239 +
240 +1:
241 +       /* is it a destination page? (r8) */
242 +       rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
243 +       beq     2f
244 +
245 +       rlwinm  r8, r0, 0, 0, 19 /* clear kexec flags, page align */
246 +       b       0b
247 +
248 +2:     /* is it an indirection page? (r3) */
249 +       rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
250 +       beq     2f
251 +
252 +       rlwinm  r3, r0, 0, 0, 19 /* clear kexec flags, page align */
253 +       subi    r3, r3, 4
254 +       b       0b
255 +
256 +2:     /* are we done? */
257 +       rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
258 +       beq     2f
259 +       b       3f
260 +
261 +2:     /* is it a source page? (r9) */
262 +       rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
263 +       beq     0b
264 +
265 +       rlwinm  r9, r0, 0, 0, 19 /* clear kexec flags, page align */
266 +
267 +       li      r7, PAGE_SIZE / 4
268 +       mtctr   r7
269 +       subi    r9, r9, 4
270 +       subi    r8, r8, 4
271 +9:
272 +       lwzu    r0, 4(r9)  /* do the copy */
273 +       xor     r6, r6, r0
274 +       stwu    r0, 4(r8)
275 +       dcbst   0, r8
276 +       sync
277 +       icbi    0, r8
278 +       bdnz    9b
279 +
280 +       addi    r9, r9, 4
281 +       addi    r8, r8, 4
282 +       b       0b
283 +
284 +3:
285 +
286 +       /* To be certain of avoiding problems with self-modifying code
287 +        * execute a serializing instruction here.
288 +        */
289 +       isync
290 +       sync
291 +
292 +       /* jump to the entry point, usually the setup routine */
293 +       mtlr    r5
294 +       blrl
295 +
296 +1:     b       1b
297 +
298 +relocate_new_kernel_end:
299 +
300 +       .globl relocate_new_kernel_size
301 +relocate_new_kernel_size:
302 +       .long relocate_new_kernel_end - relocate_new_kernel
303 +
304 diff -Narup linux-2.4.31-orig/arch/ppc/platforms/redwood6.c linux-2.4.31/arch/ppc/platforms/redwood6.c
305 --- linux-2.4.31-orig/arch/ppc/platforms/redwood6.c     2004-04-14 06:05:27.000000000 -0700
306 +++ linux-2.4.31/arch/ppc/platforms/redwood6.c  2005-08-24 22:57:12.000000000 -0700
307 @@ -168,7 +168,26 @@ board_setup_irq(void)
308  {
309  }
310  
311 +#ifdef CONFIG_KEXEC
312 +int redwood6_kexec_prepare(struct kimage *image)
313 +{
314 +       /* here, we can place additional preparations */
315 +       return 0; /* yes, we support kexec */
316 +}
317 +                                                      
318 +                         
319 +void redwood6_kexec(struct kimage *image)
320 +{
321 +       /* just call the simple kexec version... */
322 +       machine_kexec_simple(image);
323 +}
324 +#endif /* CONFIG_KEXEC */
325 +
326  void __init
327  board_init(void)
328  {
329 +#ifdef CONFIG_KEXEC
330 +       ppc_md.machine_kexec_prepare = redwood6_kexec_prepare;
331 +       ppc_md.machine_kexec = redwood6_kexec;
332 +#endif
333  }
334 diff -Narup linux-2.4.31-orig/include/asm-ppc/io.h linux-2.4.31/include/asm-ppc/io.h
335 --- linux-2.4.31-orig/include/asm-ppc/io.h      2003-11-28 10:26:21.000000000 -0800
336 +++ linux-2.4.31/include/asm-ppc/io.h   2005-08-24 23:28:30.000000000 -0700
337 @@ -275,6 +275,10 @@ extern inline void * phys_to_virt(unsign
338  #define page_to_phys(page)     (((page - mem_map) << PAGE_SHIFT) + PPC_MEMSTART)
339  #define page_to_bus(page)      (page_to_phys(page) + PCI_DRAM_OFFSET)
340  
341 +/* added for kexec support */
342 +#define pfn_to_page(pfn)       (mem_map + ((pfn) - PPC_PGSTART))
343 +#define page_to_pfn(page)      ((unsigned long)((page) - mem_map) + PPC_PGSTART)
344 +
345  /*
346   * Enforce In-order Execution of I/O:
347   * Acts as a barrier to ensure all previous I/O accesses have
348 diff -Narup linux-2.4.31-orig/include/asm-ppc/kexec.h linux-2.4.31/include/asm-ppc/kexec.h
349 --- linux-2.4.31-orig/include/asm-ppc/kexec.h   1969-12-31 16:00:00.000000000 -0800
350 +++ linux-2.4.31/include/asm-ppc/kexec.h        2005-08-24 22:58:28.000000000 -0700
351 @@ -0,0 +1,38 @@
352 +#ifndef _PPC_KEXEC_H
353 +#define _PPC_KEXEC_H
354 +
355 +#ifdef CONFIG_KEXEC
356 +
357 +/*
358 + * KEXEC_SOURCE_MEMORY_LIMIT maximum page get_free_page can return.
359 + * I.e. Maximum page that is mapped directly into kernel memory,
360 + * and kmap is not required.
361 + *
362 + * Someone correct me if FIXADDR_START - PAGEOFFSET is not the correct
363 + * calculation for the amount of memory directly mappable into the
364 + * kernel memory space.
365 + */
366 +
367 +/* Maximum physical address we can use pages from */
368 +#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
369 +/* Maximum address we can reach in physical address mode */
370 +#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
371 +/* Maximum address we can use for the control code buffer */
372 +#define KEXEC_CONTROL_MEMORY_LIMIT TASK_SIZE
373 +
374 +#define KEXEC_CONTROL_CODE_SIZE        4096
375 +
376 +/* The native architecture */
377 +#define KEXEC_ARCH KEXEC_ARCH_PPC
378 +
379 +#ifndef __ASSEMBLY__
380 +
381 +struct kimage;
382 +
383 +extern void machine_kexec_simple(struct kimage *image);
384 +
385 +#endif /* __ASSEMBLY__ */
386 +
387 +#endif /* CONFIG_KEXEC */
388 +
389 +#endif /* _PPC_KEXEC_H */
390 diff -Narup linux-2.4.31-orig/include/asm-ppc/machdep.h linux-2.4.31/include/asm-ppc/machdep.h
391 --- linux-2.4.31-orig/include/asm-ppc/machdep.h 2005-08-24 22:05:10.000000000 -0700
392 +++ linux-2.4.31/include/asm-ppc/machdep.h      2005-08-24 23:41:01.000000000 -0700
393 @@ -3,6 +3,7 @@
394  #define _PPC_MACHDEP_H
395  
396  #include <linux/config.h>
397 +#include <linux/kexec.h>
398  
399  #ifdef CONFIG_APUS
400  #include <asm-m68k/machdep.h>
401 @@ -113,6 +114,36 @@ struct machdep_calls {
402         /* functions for dealing with other cpus */
403         struct smp_ops_t *smp_ops;
404  #endif /* CONFIG_SMP */
405 +
406 +#ifdef CONFIG_KEXEC
407 +       /* Called to shutdown machine specific hardware not already controlled
408 +        * by other drivers.
409 +        * XXX Should we move this one out of kexec scope?
410 +        */
411 +       void (*machine_shutdown)(void);
412 +
413 +       /* Called to do the minimal shutdown needed to run a kexec'd kernel
414 +        * to run successfully.
415 +        * XXX Should we move this one out of kexec scope?
416 +        */
417 +       void (*machine_crash_shutdown)(void);
418 +
419 +       /* Called to do what every setup is needed on image and the
420 +        * reboot code buffer. Returns 0 on success.
421 +        * Provide your own (maybe dummy) implementation if your platform
422 +        * claims to support kexec.
423 +        */
424 +       int (*machine_kexec_prepare)(struct kimage *image);
425 +
426 +       /* Called to handle any machine specific cleanup on image */
427 +       void (*machine_kexec_cleanup)(struct kimage *image);
428 +
429 +       /* Called to perform the _real_ kexec.
430 +        * Do NOT allocate memory or fail here. We are past the point of
431 +        * no return.
432 +        */
433 +       void (*machine_kexec)(struct kimage *image);
434 +#endif /* CONFIG_KEXEC */
435  };
436  
437  extern struct machdep_calls ppc_md;
438 diff -Narup linux-2.4.31-orig/include/asm-ppc/page.h linux-2.4.31/include/asm-ppc/page.h
439 --- linux-2.4.31-orig/include/asm-ppc/page.h    2003-11-28 10:26:21.000000000 -0800
440 +++ linux-2.4.31/include/asm-ppc/page.h 2005-08-24 23:27:19.000000000 -0700
441 @@ -104,6 +104,7 @@ extern unsigned long ppc_memstart;
442  extern unsigned long ppc_memoffset;
443  #ifndef CONFIG_APUS
444  #define PPC_MEMSTART   0
445 +#define PPC_PGSTART            0
446  #define PPC_MEMOFFSET  PAGE_OFFSET
447  #else
448  #define PPC_MEMSTART   ppc_memstart
449 diff -Narup linux-2.4.31-orig/include/asm-ppc/unistd.h linux-2.4.31/include/asm-ppc/unistd.h
450 --- linux-2.4.31-orig/include/asm-ppc/unistd.h  2004-11-17 03:54:22.000000000 -0800
451 +++ linux-2.4.31/include/asm-ppc/unistd.h       2005-08-24 22:49:47.000000000 -0700
452 @@ -257,6 +257,8 @@
453  #endif
454  #define __NR_swapcontext       249
455  
456 +#define __NR_kexec_load                268
457 +
458  #define __NR(n)        #n
459  
460  /* On powerpc a system call basically clobbers the same registers like a
461 diff -Narup linux-2.4.31-orig/include/linux/kexec.h linux-2.4.31/include/linux/kexec.h
462 --- linux-2.4.31-orig/include/linux/kexec.h     1969-12-31 16:00:00.000000000 -0800
463 +++ linux-2.4.31/include/linux/kexec.h  2005-08-24 23:40:30.000000000 -0700
464 @@ -0,0 +1,115 @@
465 +#ifndef LINUX_KEXEC_H
466 +#define LINUX_KEXEC_H
467 +
468 +#ifdef CONFIG_KEXEC
469 +#include <linux/compiler.h>
470 +#include <linux/kernel.h>
471 +#include <linux/types.h>
472 +#include <linux/list.h>
473 +#include <linux/linkage.h>
474 +#include <asm/kexec.h>
475 +
476 +/* Verify architecture specific macros are defined */
477 +
478 +#ifndef KEXEC_SOURCE_MEMORY_LIMIT
479 +#error KEXEC_SOURCE_MEMORY_LIMIT not defined
480 +#endif
481 +
482 +#ifndef KEXEC_DESTINATION_MEMORY_LIMIT
483 +#error KEXEC_DESTINATION_MEMORY_LIMIT not defined
484 +#endif
485 +
486 +#ifndef KEXEC_CONTROL_MEMORY_LIMIT
487 +#error KEXEC_CONTROL_MEMORY_LIMIT not defined
488 +#endif
489 +
490 +#ifndef KEXEC_CONTROL_CODE_SIZE
491 +#error KEXEC_CONTROL_CODE_SIZE not defined
492 +#endif
493 +
494 +#ifndef KEXEC_ARCH
495 +#error KEXEC_ARCH not defined
496 +#endif
497 +
498 +/*
499 + * This structure is used to hold the arguments that are used when loading
500 + * kernel binaries.
501 + */
502 +
503 +typedef unsigned long kimage_entry_t;
504 +#define IND_DESTINATION  0x1
505 +#define IND_INDIRECTION  0x2
506 +#define IND_DONE         0x4
507 +#define IND_SOURCE       0x8
508 +
509 +#define KEXEC_SEGMENT_MAX 8
510 +struct kexec_segment {
511 +       void __user *buf;
512 +       size_t bufsz;
513 +       unsigned long mem;      /* User space sees this as a (void *) ... */
514 +       size_t memsz;
515 +};
516 +
517 +struct kimage {
518 +       kimage_entry_t head;
519 +       kimage_entry_t *entry;
520 +       kimage_entry_t *last_entry;
521 +
522 +       unsigned long destination;
523 +
524 +       unsigned long start;
525 +       struct page *control_code_page;
526 +
527 +       unsigned long nr_segments;
528 +       struct kexec_segment segment[KEXEC_SEGMENT_MAX];
529 +
530 +       struct list_head control_pages;
531 +       struct list_head dest_pages;
532 +       struct list_head unuseable_pages;
533 +
534 +       /* Address of next control page to allocate for crash kernels. */
535 +       unsigned long control_page;
536 +
537 +       /* Flags to indicate special processing */
538 +       unsigned int type : 1;
539 +#define KEXEC_TYPE_DEFAULT 0
540 +#define KEXEC_TYPE_CRASH   1
541 +};
542 +
543 +
544 +
545 +/* kexec interface functions */
546 +extern NORET_TYPE void machine_kexec(struct kimage *image) ATTRIB_NORET;
547 +extern int machine_kexec_prepare(struct kimage *image);
548 +extern void machine_kexec_cleanup(struct kimage *image);
549 +extern asmlinkage long sys_kexec_load(unsigned long entry,
550 +       unsigned long nr_segments, struct kexec_segment __user *segments,
551 +       unsigned long flags);
552 +extern struct page *kimage_alloc_control_pages(struct kimage *image, unsigned int order);
553 +extern void crash_kexec(void);
554 +extern struct kimage *kexec_image;
555 +extern struct kimage *kexec_crash_image;
556 +
557 +#define KEXEC_ON_CRASH  0x00000001
558 +#define KEXEC_ARCH_MASK 0xffff0000
559 +
560 +/* These values match the ELF architecture values.
561 + * Unless there is a good reason that should continue to be the case.
562 + */
563 +#define KEXEC_ARCH_DEFAULT ( 0 << 16)
564 +#define KEXEC_ARCH_386     ( 3 << 16)
565 +#define KEXEC_ARCH_X86_64  (62 << 16)
566 +#define KEXEC_ARCH_PPC     (20 << 16)
567 +#define KEXEC_ARCH_PPC64   (21 << 16)
568 +#define KEXEC_ARCH_IA_64   (50 << 16)
569 +
570 +#define KEXEC_FLAGS    (KEXEC_ON_CRASH)  /* List of defined/legal kexec flags */
571 +
572 +/* Location of a reserved region to hold the crash kernel.
573 + */
574 +extern struct resource crashk_res;
575 +
576 +#else /* !CONFIG_KEXEC */
577 +static inline void crash_kexec(void) { }
578 +#endif /* CONFIG_KEXEC */
579 +#endif /* LINUX_KEXEC_H */
580 diff -Narup linux-2.4.31-orig/include/linux/mm.h linux-2.4.31/include/linux/mm.h
581 --- linux-2.4.31-orig/include/linux/mm.h        2005-01-19 06:10:12.000000000 -0800
582 +++ linux-2.4.31/include/linux/mm.h     2005-08-24 23:27:57.000000000 -0700
583 @@ -165,6 +165,8 @@ typedef struct page {
584         struct page **pprev_hash;       /* Complement to *next_hash. */
585         struct buffer_head * buffers;   /* Buffer maps us to a disk block. */
586  
587 +       unsigned long private; /* added for kexec */
588 +       
589         /*
590          * On machines where all RAM is mapped into kernel address space,
591          * we can simply calculate the virtual address. On machines with
592 diff -Narup linux-2.4.31-orig/include/linux/reboot.h linux-2.4.31/include/linux/reboot.h
593 --- linux-2.4.31-orig/include/linux/reboot.h    2001-02-09 14:46:13.000000000 -0800
594 +++ linux-2.4.31/include/linux/reboot.h 2005-08-24 23:00:54.000000000 -0700
595 @@ -20,6 +20,7 @@
596   * CAD_OFF     Ctrl-Alt-Del sequence sends SIGINT to init task.
597   * POWER_OFF   Stop OS and remove all power from system, if possible.
598   * RESTART2    Restart system using given command string.
599 + * KEXEC       Restart system using a previously loaded Linux kernel
600   */
601  
602  #define        LINUX_REBOOT_CMD_RESTART        0x01234567
603 @@ -28,6 +29,7 @@
604  #define        LINUX_REBOOT_CMD_CAD_OFF        0x00000000
605  #define        LINUX_REBOOT_CMD_POWER_OFF      0x4321FEDC
606  #define        LINUX_REBOOT_CMD_RESTART2       0xA1B2C3D4
607 +#define        LINUX_REBOOT_CMD_KEXEC          0x45584543
608  
609  
610  #ifdef __KERNEL__
611 @@ -45,6 +47,8 @@ extern int unregister_reboot_notifier(st
612  extern void machine_restart(char *cmd);
613  extern void machine_halt(void);
614  extern void machine_power_off(void);
615 +extern void machine_shutdown(void);
616 +extern void machine_crash_shutdown(void);
617  
618  #endif
619  
620 diff -Narup linux-2.4.31-orig/kernel/Makefile linux-2.4.31/kernel/Makefile
621 --- linux-2.4.31-orig/kernel/Makefile   2001-09-16 21:22:40.000000000 -0700
622 +++ linux-2.4.31/kernel/Makefile        2005-08-24 22:38:41.000000000 -0700
623 @@ -19,6 +19,7 @@ obj-y     = sched.o dma.o fork.o exec_do
624  obj-$(CONFIG_UID16) += uid16.o
625  obj-$(CONFIG_MODULES) += ksyms.o
626  obj-$(CONFIG_PM) += pm.o
627 +obj-$(CONFIG_KEXEC) += kexec.o
628  
629  ifneq ($(CONFIG_IA64),y)
630  # According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
631 diff -Narup linux-2.4.31-orig/kernel/kexec.c linux-2.4.31/kernel/kexec.c
632 --- linux-2.4.31-orig/kernel/kexec.c    1969-12-31 16:00:00.000000000 -0800
633 +++ linux-2.4.31/kernel/kexec.c 2005-08-24 23:30:47.000000000 -0700
634 @@ -0,0 +1,993 @@
635 +/*
636 + * kexec.c - kexec system call
637 + * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
638 + *
639 + * This source code is licensed under the GNU General Public License,
640 + * Version 2.  See the file COPYING for more details.
641 + */
642 +
643 +#include <linux/mm.h>
644 +#include <linux/file.h>
645 +#include <linux/slab.h>
646 +#include <linux/fs.h>
647 +#include <linux/kexec.h>
648 +#include <linux/spinlock.h>
649 +#include <linux/list.h>
650 +#include <linux/highmem.h>
651 +#include <linux/reboot.h>
652 +//#include <linux/syscalls.h>
653 +#include <linux/ioport.h>
654 +#include <asm/page.h>
655 +#include <asm/uaccess.h>
656 +#include <asm/io.h>
657 +#include <asm/system.h>
658 +#include <asm/semaphore.h>
659 +
660 +/* Location of the reserved area for the crash kernel */
661 +struct resource crashk_res = {
662 +       .name  = "Crash kernel",
663 +       .start = 0,
664 +       .end   = 0,
665 +       .flags = IORESOURCE_BUSY | IORESOURCE_MEM
666 +};
667 +
668 +/*
669 + * When kexec transitions to the new kernel there is a one-to-one
670 + * mapping between physical and virtual addresses.  On processors
671 + * where you can disable the MMU this is trivial, and easy.  For
672 + * others it is still a simple predictable page table to setup.
673 + *
674 + * In that environment kexec copies the new kernel to its final
675 + * resting place.  This means I can only support memory whose
676 + * physical address can fit in an unsigned long.  In particular
677 + * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
678 + * If the assembly stub has more restrictive requirements
679 + * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
680 + * defined more restrictively in <asm/kexec.h>.
681 + *
682 + * The code for the transition from the current kernel to the
683 + * the new kernel is placed in the control_code_buffer, whose size
684 + * is given by KEXEC_CONTROL_CODE_SIZE.  In the best case only a single
685 + * page of memory is necessary, but some architectures require more.
686 + * Because this memory must be identity mapped in the transition from
687 + * virtual to physical addresses it must live in the range
688 + * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
689 + * modifiable.
690 + *
691 + * The assembly stub in the control code buffer is passed a linked list
692 + * of descriptor pages detailing the source pages of the new kernel,
693 + * and the destination addresses of those source pages.  As this data
694 + * structure is not used in the context of the current OS, it must
695 + * be self-contained.
696 + *
697 + * The code has been made to work with highmem pages and will use a
698 + * destination page in its final resting place (if it happens
699 + * to allocate it).  The end product of this is that most of the
700 + * physical address space, and most of RAM can be used.
701 + *
702 + * Future directions include:
703 + *  - allocating a page table with the control code buffer identity
704 + *    mapped, to simplify machine_kexec and make kexec_on_panic more
705 + *    reliable.
706 + */
707 +
708 +/*
709 + * KIMAGE_NO_DEST is an impossible destination address..., for
710 + * allocating pages whose destination address we do not care about.
711 + */
712 +#define KIMAGE_NO_DEST (-1UL)
713 +
714 +static int kimage_is_destination_range(
715 +       struct kimage *image, unsigned long start, unsigned long end);
716 +static struct page *kimage_alloc_page(struct kimage *image, unsigned int gfp_mask, unsigned long dest);
717 +
718 +static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
719 +       unsigned long nr_segments, struct kexec_segment __user *segments)
720 +{
721 +       size_t segment_bytes;
722 +       struct kimage *image;
723 +       unsigned long i;
724 +       int result;
725 +
726 +       /* Allocate a controlling structure */
727 +       result = -ENOMEM;
728 +       image = kmalloc(sizeof(*image), GFP_KERNEL);
729 +       if (!image) {
730 +               goto out;
731 +       }
732 +       memset(image, 0, sizeof(*image));
733 +       image->head = 0;
734 +       image->entry = &image->head;
735 +       image->last_entry = &image->head;
736 +       image->control_page = ~0; /* By default this does not apply */
737 +       image->start = entry;
738 +       image->type = KEXEC_TYPE_DEFAULT;
739 +
740 +       /* Initialize the list of control pages */
741 +       INIT_LIST_HEAD(&image->control_pages);
742 +
743 +       /* Initialize the list of destination pages */
744 +       INIT_LIST_HEAD(&image->dest_pages);
745 +
746 +       /* Initialize the list of unuseable pages */
747 +       INIT_LIST_HEAD(&image->unuseable_pages);
748 +
749 +       /* Read in the segments */
750 +       image->nr_segments = nr_segments;
751 +       segment_bytes = nr_segments * sizeof(*segments);
752 +       result = copy_from_user(image->segment, segments, segment_bytes);
753 +       if (result)
754 +               goto out;
755 +
756 +       /*
757 +        * Verify we have good destination addresses.  The caller is
758 +        * responsible for making certain we don't attempt to load
759 +        * the new image into invalid or reserved areas of RAM.  This
760 +        * just verifies it is an address we can use.
761 +        *
762 +        * Since the kernel does everything in page size chunks ensure
763 +        * the destination addreses are page aligned.  Too many
764 +        * special cases crop of when we don't do this.  The most
765 +        * insidious is getting overlapping destination addresses
766 +        * simply because addresses are changed to page size
767 +        * granularity.
768 +        */
769 +       result = -EADDRNOTAVAIL;
770 +       for (i = 0; i < nr_segments; i++) {
771 +               unsigned long mstart, mend;
772 +               mstart = image->segment[i].mem;
773 +               mend   = mstart + image->segment[i].memsz;
774 +               if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
775 +                       goto out;
776 +               if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
777 +                       goto out;
778 +       }
779 +
780 +       /* Verify our destination addresses do not overlap.
781 +        * If we alloed overlapping destination addresses
782 +        * through very weird things can happen with no
783 +        * easy explanation as one segment stops on another.
784 +        */
785 +       result = -EINVAL;
786 +       for(i = 0; i < nr_segments; i++) {
787 +               unsigned long mstart, mend;
788 +               unsigned long j;
789 +               mstart = image->segment[i].mem;
790 +               mend   = mstart + image->segment[i].memsz;
791 +               for(j = 0; j < i; j++) {
792 +                       unsigned long pstart, pend;
793 +                       pstart = image->segment[j].mem;
794 +                       pend   = pstart + image->segment[j].memsz;
795 +                       /* Do the segments overlap ? */
796 +                       if ((mend > pstart) && (mstart < pend))
797 +                               goto out;
798 +               }
799 +       }
800 +
801 +       /* Ensure our buffer sizes are strictly less than
802 +        * our memory sizes.  This should always be the case,
803 +        * and it is easier to check up front than to be surprised
804 +        * later on.
805 +        */
806 +       result = -EINVAL;
807 +       for(i = 0; i < nr_segments; i++) {
808 +               if (image->segment[i].bufsz > image->segment[i].memsz)
809 +                       goto out;
810 +       }
811 +
812 +
813 +       result = 0;
814 + out:
815 +       if (result == 0) {
816 +               *rimage = image;
817 +       } else {
818 +               kfree(image);
819 +       }
820 +       return result;
821 +
822 +}
823 +
824 +static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
825 +       unsigned long nr_segments, struct kexec_segment __user *segments)
826 +{
827 +       int result;
828 +       struct kimage *image;
829 +
830 +       /* Allocate and initialize a controlling structure */
831 +       image = NULL;
832 +       result = do_kimage_alloc(&image, entry, nr_segments, segments);
833 +       if (result) {
834 +               goto out;
835 +       }
836 +       *rimage = image;
837 +
838 +       /*
839 +        * Find a location for the control code buffer, and add it
840 +        * the vector of segments so that it's pages will also be
841 +        * counted as destination pages.
842 +        */
843 +       result = -ENOMEM;
844 +       image->control_code_page = kimage_alloc_control_pages(image,
845 +               get_order(KEXEC_CONTROL_CODE_SIZE));
846 +       if (!image->control_code_page) {
847 +               printk(KERN_ERR "Could not allocate control_code_buffer\n");
848 +               goto out;
849 +       }
850 +
851 +       result = 0;
852 + out:
853 +       if (result == 0) {
854 +               *rimage = image;
855 +       } else {
856 +               kfree(image);
857 +       }
858 +       return result;
859 +}
860 +
861 +static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
862 +       unsigned long nr_segments, struct kexec_segment *segments)
863 +{
864 +       int result;
865 +       struct kimage *image;
866 +       unsigned long i;
867 +
868 +       image = NULL;
869 +       /* Verify we have a valid entry point */
870 +       if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
871 +               result = -EADDRNOTAVAIL;
872 +               goto out;
873 +       }
874 +
875 +       /* Allocate and initialize a controlling structure */
876 +       result = do_kimage_alloc(&image, entry, nr_segments, segments);
877 +       if (result) {
878 +               goto out;
879 +       }
880 +
881 +       /* Enable the special crash kernel control page
882 +        * allocation policy.
883 +        */
884 +       image->control_page = crashk_res.start;
885 +       image->type = KEXEC_TYPE_CRASH;
886 +
887 +       /*
888 +        * Verify we have good destination addresses.  Normally
889 +        * the caller is responsible for making certain we don't
890 +        * attempt to load the new image into invalid or reserved
891 +        * areas of RAM.  But crash kernels are preloaded into a
892 +        * reserved area of ram.  We must ensure the addresses
893 +        * are in the reserved area otherwise preloading the
894 +        * kernel could corrupt things.
895 +        */
896 +       result = -EADDRNOTAVAIL;
897 +       for (i = 0; i < nr_segments; i++) {
898 +               unsigned long mstart, mend;
899 +               mstart = image->segment[i].mem;
900 +               mend = mstart + image->segment[i].memsz;
901 +               /* Ensure we are within the crash kernel limits */
902 +               if ((mstart < crashk_res.start) || (mend > crashk_res.end))
903 +                       goto out;
904 +       }
905 +
906 +
907 +       /*
908 +        * Find a location for the control code buffer, and add
909 +        * the vector of segments so that it's pages will also be
910 +        * counted as destination pages.
911 +        */
912 +       result = -ENOMEM;
913 +       image->control_code_page = kimage_alloc_control_pages(image,
914 +               get_order(KEXEC_CONTROL_CODE_SIZE));
915 +       if (!image->control_code_page) {
916 +               printk(KERN_ERR "Could not allocate control_code_buffer\n");
917 +               goto out;
918 +       }
919 +
920 +       result = 0;
921 + out:
922 +       if (result == 0) {
923 +               *rimage = image;
924 +       } else {
925 +               kfree(image);
926 +       }
927 +       return result;
928 +}
929 +
930 +static int kimage_is_destination_range(
931 +       struct kimage *image, unsigned long start, unsigned long end)
932 +{
933 +       unsigned long i;
934 +
935 +       for (i = 0; i < image->nr_segments; i++) {
936 +               unsigned long mstart, mend;
937 +               mstart = image->segment[i].mem;
938 +               mend   = mstart + image->segment[i].memsz;
939 +               if ((end > mstart) && (start < mend)) {
940 +                       return 1;
941 +               }
942 +       }
943 +       return 0;
944 +}
945 +
946 +static struct page *kimage_alloc_pages(unsigned int gfp_mask, unsigned int order)
947 +{
948 +       struct page *pages;
949 +       pages = alloc_pages(gfp_mask, order);
950 +       if (pages) {
951 +               unsigned int count, i;
952 +               pages->mapping = NULL;
953 +               pages->private = order;
954 +               count = 1 << order;
955 +               for(i = 0; i < count; i++) {
956 +                       SetPageReserved(pages + i);
957 +               }
958 +       }
959 +       return pages;
960 +}
961 +
962 +static void kimage_free_pages(struct page *page)
963 +{
964 +       unsigned int order, count, i;
965 +       order = page->private;
966 +       count = 1 << order;
967 +       for(i = 0; i < count; i++) {
968 +               ClearPageReserved(page + i);
969 +       }
970 +       __free_pages(page, order);
971 +}
972 +
973 +static void kimage_free_page_list(struct list_head *list)
974 +{
975 +       struct list_head *pos, *next;
976 +       list_for_each_safe(pos, next, list) {
977 +               struct page *page;
978 +
979 +               page = list_entry(pos, struct page, lru);
980 +               list_del(&page->lru);
981 +
982 +               kimage_free_pages(page);
983 +       }
984 +}
985 +
986 +static struct page *kimage_alloc_normal_control_pages(
987 +       struct kimage *image, unsigned int order)
988 +{
989 +       /* Control pages are special, they are the intermediaries
990 +        * that are needed while we copy the rest of the pages
991 +        * to their final resting place.  As such they must
992 +        * not conflict with either the destination addresses
993 +        * or memory the kernel is already using.
994 +        *
995 +        * The only case where we really need more than one of
996 +        * these are for architectures where we cannot disable
997 +        * the MMU and must instead generate an identity mapped
998 +        * page table for all of the memory.
999 +        *
1000 +        * At worst this runs in O(N) of the image size.
1001 +        */
1002 +       struct list_head extra_pages;
1003 +       struct page *pages;
1004 +       unsigned int count;
1005 +
1006 +       count = 1 << order;
1007 +       INIT_LIST_HEAD(&extra_pages);
1008 +
1009 +       /* Loop while I can allocate a page and the page allocated
1010 +        * is a destination page.
1011 +        */
1012 +       do {
1013 +               unsigned long pfn, epfn, addr, eaddr;
1014 +               pages = kimage_alloc_pages(GFP_KERNEL, order);
1015 +               if (!pages)
1016 +                       break;
1017 +               pfn   = page_to_pfn(pages);
1018 +               epfn  = pfn + count;
1019 +               addr  = pfn << PAGE_SHIFT;
1020 +               eaddr = epfn << PAGE_SHIFT;
1021 +               if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
1022 +                       kimage_is_destination_range(image, addr, eaddr))
1023 +               {
1024 +                       list_add(&pages->lru, &extra_pages);
1025 +                       pages = NULL;
1026 +               }
1027 +       } while(!pages);
1028 +       if (pages) {
1029 +               /* Remember the allocated page... */
1030 +               list_add(&pages->lru, &image->control_pages);
1031 +
1032 +               /* Because the page is already in it's destination
1033 +                * location we will never allocate another page at
1034 +                * that address.  Therefore kimage_alloc_pages
1035 +                * will not return it (again) and we don't need
1036 +                * to give it an entry in image->segment[].
1037 +                */
1038 +       }
1039 +       /* Deal with the destination pages I have inadvertently allocated.
1040 +        *
1041 +        * Ideally I would convert multi-page allocations into single
1042 +        * page allocations, and add everyting to image->dest_pages.
1043 +        *
1044 +        * For now it is simpler to just free the pages.
1045 +        */
1046 +       kimage_free_page_list(&extra_pages);
1047 +       return pages;
1048 +
1049 +}
1050 +
1051 +static struct page *kimage_alloc_crash_control_pages(
1052 +       struct kimage *image, unsigned int order)
1053 +{
1054 +       /* Control pages are special, they are the intermediaries
1055 +        * that are needed while we copy the rest of the pages
1056 +        * to their final resting place.  As such they must
1057 +        * not conflict with either the destination addresses
1058 +        * or memory the kernel is already using.
1059 +        *
1060 +        * Control pages are also the only pags we must allocate
1061 +        * when loading a crash kernel.  All of the other pages
1062 +        * are specified by the segments and we just memcpy
1063 +        * into them directly.
1064 +        *
1065 +        * The only case where we really need more than one of
1066 +        * these are for architectures where we cannot disable
1067 +        * the MMU and must instead generate an identity mapped
1068 +        * page table for all of the memory.
1069 +        *
1070 +        * Given the low demand this implements a very simple
1071 +        * allocator that finds the first hole of the appropriate
1072 +        * size in the reserved memory region, and allocates all
1073 +        * of the memory up to and including the hole.
1074 +        */
1075 +       unsigned long hole_start, hole_end, size;
1076 +       struct page *pages;
1077 +       pages = NULL;
1078 +       size = (1 << order) << PAGE_SHIFT;
1079 +       hole_start = (image->control_page + (size - 1)) & ~(size - 1);
1080 +       hole_end   = hole_start + size - 1;
1081 +       while(hole_end <= crashk_res.end) {
1082 +               unsigned long i;
1083 +               if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT) {
1084 +                       break;
1085 +               }
1086 +               if (hole_end > crashk_res.end) {
1087 +                       break;
1088 +               }
1089 +               /* See if I overlap any of the segments */
1090 +               for(i = 0; i < image->nr_segments; i++) {
1091 +                       unsigned long mstart, mend;
1092 +                       mstart = image->segment[i].mem;
1093 +                       mend   = mstart + image->segment[i].memsz - 1;
1094 +                       if ((hole_end >= mstart) && (hole_start <= mend)) {
1095 +                               /* Advance the hole to the end of the segment */
1096 +                               hole_start = (mend + (size - 1)) & ~(size - 1);
1097 +                               hole_end   = hole_start + size - 1;
1098 +                               break;
1099 +                       }
1100 +               }
1101 +               /* If I don't overlap any segments I have found my hole! */
1102 +               if (i == image->nr_segments) {
1103 +                       pages = pfn_to_page(hole_start >> PAGE_SHIFT);
1104 +                       break;
1105 +               }
1106 +       }
1107 +       if (pages) {
1108 +               image->control_page = hole_end;
1109 +       }
1110 +       return pages;
1111 +}
1112 +
1113 +
1114 +struct page *kimage_alloc_control_pages(
1115 +       struct kimage *image, unsigned int order)
1116 +{
1117 +       struct page *pages = NULL;
1118 +       switch(image->type) {
1119 +       case KEXEC_TYPE_DEFAULT:
1120 +               pages = kimage_alloc_normal_control_pages(image, order);
1121 +               break;
1122 +       case KEXEC_TYPE_CRASH:
1123 +               pages = kimage_alloc_crash_control_pages(image, order);
1124 +               break;
1125 +       }
1126 +       return pages;
1127 +}
1128 +
1129 +static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
1130 +{
1131 +       if (*image->entry != 0) {
1132 +               image->entry++;
1133 +       }
1134 +       if (image->entry == image->last_entry) {
1135 +               kimage_entry_t *ind_page;
1136 +               struct page *page;
1137 +               page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
1138 +               if (!page) {
1139 +                       return -ENOMEM;
1140 +               }
1141 +               ind_page = page_address(page);
1142 +               *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
1143 +               image->entry = ind_page;
1144 +               image->last_entry =
1145 +                       ind_page + ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
1146 +       }
1147 +       *image->entry = entry;
1148 +       image->entry++;
1149 +       *image->entry = 0;
1150 +       return 0;
1151 +}
1152 +
1153 +static int kimage_set_destination(
1154 +       struct kimage *image, unsigned long destination)
1155 +{
1156 +       int result;
1157 +
1158 +       destination &= PAGE_MASK;
1159 +       result = kimage_add_entry(image, destination | IND_DESTINATION);
1160 +       if (result == 0) {
1161 +               image->destination = destination;
1162 +       }
1163 +       return result;
1164 +}
1165 +
1166 +
1167 +static int kimage_add_page(struct kimage *image, unsigned long page)
1168 +{
1169 +       int result;
1170 +
1171 +       page &= PAGE_MASK;
1172 +       result = kimage_add_entry(image, page | IND_SOURCE);
1173 +       if (result == 0) {
1174 +               image->destination += PAGE_SIZE;
1175 +       }
1176 +       return result;
1177 +}
1178 +
1179 +
1180 +static void kimage_free_extra_pages(struct kimage *image)
1181 +{
1182 +       /* Walk through and free any extra destination pages I may have */
1183 +       kimage_free_page_list(&image->dest_pages);
1184 +
1185 +       /* Walk through and free any unuseable pages I have cached */
1186 +       kimage_free_page_list(&image->unuseable_pages);
1187 +
1188 +}
1189 +static int kimage_terminate(struct kimage *image)
1190 +{
1191 +       if (*image->entry != 0) {
1192 +               image->entry++;
1193 +       }
1194 +       *image->entry = IND_DONE;
1195 +       return 0;
1196 +}
1197 +
1198 +#define for_each_kimage_entry(image, ptr, entry) \
1199 +       for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
1200 +               ptr = (entry & IND_INDIRECTION)? \
1201 +                       phys_to_virt((entry & PAGE_MASK)): ptr +1)
1202 +
1203 +static void kimage_free_entry(kimage_entry_t entry)
1204 +{
1205 +       struct page *page;
1206 +
1207 +       page = pfn_to_page(entry >> PAGE_SHIFT);
1208 +       kimage_free_pages(page);
1209 +}
1210 +
1211 +static void kimage_free(struct kimage *image)
1212 +{
1213 +       kimage_entry_t *ptr, entry;
1214 +       kimage_entry_t ind = 0;
1215 +
1216 +       if (!image)
1217 +               return;
1218 +       kimage_free_extra_pages(image);
1219 +       for_each_kimage_entry(image, ptr, entry) {
1220 +               if (entry & IND_INDIRECTION) {
1221 +                       /* Free the previous indirection page */
1222 +                       if (ind & IND_INDIRECTION) {
1223 +                               kimage_free_entry(ind);
1224 +                       }
1225 +                       /* Save this indirection page until we are
1226 +                        * done with it.
1227 +                        */
1228 +                       ind = entry;
1229 +               }
1230 +               else if (entry & IND_SOURCE) {
1231 +                       kimage_free_entry(entry);
1232 +               }
1233 +       }
1234 +       /* Free the final indirection page */
1235 +       if (ind & IND_INDIRECTION) {
1236 +               kimage_free_entry(ind);
1237 +       }
1238 +
1239 +       /* Handle any machine specific cleanup */
1240 +       machine_kexec_cleanup(image);
1241 +
1242 +       /* Free the kexec control pages... */
1243 +       kimage_free_page_list(&image->control_pages);
1244 +       kfree(image);
1245 +}
1246 +
1247 +static kimage_entry_t *kimage_dst_used(struct kimage *image, unsigned long page)
1248 +{
1249 +       kimage_entry_t *ptr, entry;
1250 +       unsigned long destination = 0;
1251 +
1252 +       for_each_kimage_entry(image, ptr, entry) {
1253 +               if (entry & IND_DESTINATION) {
1254 +                       destination = entry & PAGE_MASK;
1255 +               }
1256 +               else if (entry & IND_SOURCE) {
1257 +                       if (page == destination) {
1258 +                               return ptr;
1259 +                       }
1260 +                       destination += PAGE_SIZE;
1261 +               }
1262 +       }
1263 +       return 0;
1264 +}
1265 +
1266 +static struct page *kimage_alloc_page(struct kimage *image, unsigned int gfp_mask, unsigned long destination)
1267 +{
1268 +       /*
1269 +        * Here we implement safeguards to ensure that a source page
1270 +        * is not copied to its destination page before the data on
1271 +        * the destination page is no longer useful.
1272 +        *
1273 +        * To do this we maintain the invariant that a source page is
1274 +        * either its own destination page, or it is not a
1275 +        * destination page at all.
1276 +        *
1277 +        * That is slightly stronger than required, but the proof
1278 +        * that no problems will not occur is trivial, and the
1279 +        * implementation is simply to verify.
1280 +        *
1281 +        * When allocating all pages normally this algorithm will run
1282 +        * in O(N) time, but in the worst case it will run in O(N^2)
1283 +        * time.   If the runtime is a problem the data structures can
1284 +        * be fixed.
1285 +        */
1286 +       struct page *page;
1287 +       unsigned long addr;
1288 +
1289 +       /*
1290 +        * Walk through the list of destination pages, and see if I
1291 +        * have a match.
1292 +        */
1293 +       list_for_each_entry(page, &image->dest_pages, lru) {
1294 +               addr = page_to_pfn(page) << PAGE_SHIFT;
1295 +               if (addr == destination) {
1296 +                       list_del(&page->lru);
1297 +                       return page;
1298 +               }
1299 +       }
1300 +       page = NULL;
1301 +       while (1) {
1302 +               kimage_entry_t *old;
1303 +
1304 +               /* Allocate a page, if we run out of memory give up */
1305 +               page = kimage_alloc_pages(gfp_mask, 0);
1306 +               if (!page) {
1307 +                       return 0;
1308 +               }
1309 +               /* If the page cannot be used file it away */
1310 +               if (page_to_pfn(page) > (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
1311 +                       list_add(&page->lru, &image->unuseable_pages);
1312 +                       continue;
1313 +               }
1314 +               addr = page_to_pfn(page) << PAGE_SHIFT;
1315 +
1316 +               /* If it is the destination page we want use it */
1317 +               if (addr == destination)
1318 +                       break;
1319 +
1320 +               /* If the page is not a destination page use it */
1321 +               if (!kimage_is_destination_range(image, addr, addr + PAGE_SIZE))
1322 +                       break;
1323 +
1324 +               /*
1325 +                * I know that the page is someones destination page.
1326 +                * See if there is already a source page for this
1327 +                * destination page.  And if so swap the source pages.
1328 +                */
1329 +               old = kimage_dst_used(image, addr);
1330 +               if (old) {
1331 +                       /* If so move it */
1332 +                       unsigned long old_addr;
1333 +                       struct page *old_page;
1334 +
1335 +                       old_addr = *old & PAGE_MASK;
1336 +                       old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
1337 +                       copy_highpage(page, old_page);
1338 +                       *old = addr | (*old & ~PAGE_MASK);
1339 +
1340 +                       /* The old page I have found cannot be a
1341 +                        * destination page, so return it.
1342 +                        */
1343 +                       addr = old_addr;
1344 +                       page = old_page;
1345 +                       break;
1346 +               }
1347 +               else {
1348 +                       /* Place the page on the destination list I
1349 +                        * will use it later.
1350 +                        */
1351 +                       list_add(&page->lru, &image->dest_pages);
1352 +               }
1353 +       }
1354 +       return page;
1355 +}
1356 +
1357 +static int kimage_load_normal_segment(struct kimage *image,
1358 +       struct kexec_segment *segment)
1359 +{
1360 +       unsigned long maddr;
1361 +       unsigned long ubytes, mbytes;
1362 +       int result;
1363 +       unsigned char *buf;
1364 +
1365 +       result = 0;
1366 +       buf = segment->buf;
1367 +       ubytes = segment->bufsz;
1368 +       mbytes = segment->memsz;
1369 +       maddr = segment->mem;
1370 +
1371 +       result = kimage_set_destination(image, maddr);
1372 +       if (result < 0) {
1373 +               goto out;
1374 +       }
1375 +       while(mbytes) {
1376 +               struct page *page;
1377 +               char *ptr;
1378 +               size_t uchunk, mchunk;
1379 +               page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
1380 +               if (page == 0) {
1381 +                       result  = -ENOMEM;
1382 +                       goto out;
1383 +               }
1384 +               result = kimage_add_page(image, page_to_pfn(page) << PAGE_SHIFT);
1385 +               if (result < 0) {
1386 +                       goto out;
1387 +               }
1388 +               ptr = kmap(page);
1389 +               /* Start with a clear page */
1390 +               memset(ptr, 0, PAGE_SIZE);
1391 +               ptr += maddr & ~PAGE_MASK;
1392 +               mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
1393 +               if (mchunk > mbytes) {
1394 +                       mchunk = mbytes;
1395 +               }
1396 +               uchunk = mchunk;
1397 +               if (uchunk > ubytes) {
1398 +                       uchunk = ubytes;
1399 +               }
1400 +               result = copy_from_user(ptr, buf, uchunk);
1401 +               kunmap(page);
1402 +               if (result) {
1403 +                       result = (result < 0) ? result : -EIO;
1404 +                       goto out;
1405 +               }
1406 +               ubytes -= uchunk;
1407 +               maddr  += mchunk;
1408 +               buf    += mchunk;
1409 +               mbytes -= mchunk;
1410 +       }
1411 + out:
1412 +       return result;
1413 +}
1414 +
1415 +static int kimage_load_crash_segment(struct kimage *image,
1416 +       struct kexec_segment *segment)
1417 +{
1418 +       /* For crash dumps kernels we simply copy the data from
1419 +        * user space to it's destination.
1420 +        * We do things a page at a time for the sake of kmap.
1421 +        */
1422 +       unsigned long maddr;
1423 +       unsigned long ubytes, mbytes;
1424 +       int result;
1425 +       unsigned char *buf;
1426 +
1427 +       result = 0;
1428 +       buf = segment->buf;
1429 +       ubytes = segment->bufsz;
1430 +       mbytes = segment->memsz;
1431 +       maddr = segment->mem;
1432 +       while(mbytes) {
1433 +               struct page *page;
1434 +               char *ptr;
1435 +               size_t uchunk, mchunk;
1436 +               page = pfn_to_page(maddr >> PAGE_SHIFT);
1437 +               if (page == 0) {
1438 +                       result  = -ENOMEM;
1439 +                       goto out;
1440 +               }
1441 +               ptr = kmap(page);
1442 +               ptr += maddr & ~PAGE_MASK;
1443 +               mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
1444 +               if (mchunk > mbytes) {
1445 +                       mchunk = mbytes;
1446 +               }
1447 +               uchunk = mchunk;
1448 +               if (uchunk > ubytes) {
1449 +                       uchunk = ubytes;
1450 +                       /* Zero the trailing part of the page */
1451 +                       memset(ptr + uchunk, 0, mchunk - uchunk);
1452 +               }
1453 +               result = copy_from_user(ptr, buf, uchunk);
1454 +               kunmap(page);
1455 +               if (result) {
1456 +                       result = (result < 0) ? result : -EIO;
1457 +                       goto out;
1458 +               }
1459 +               ubytes -= uchunk;
1460 +               maddr  += mchunk;
1461 +               buf    += mchunk;
1462 +               mbytes -= mchunk;
1463 +       }
1464 + out:
1465 +       return result;
1466 +}
1467 +
1468 +static int kimage_load_segment(struct kimage *image,
1469 +       struct kexec_segment *segment)
1470 +{
1471 +       int result = -ENOMEM;
1472 +       switch(image->type) {
1473 +       case KEXEC_TYPE_DEFAULT:
1474 +               result = kimage_load_normal_segment(image, segment);
1475 +               break;
1476 +       case KEXEC_TYPE_CRASH:
1477 +               result = kimage_load_crash_segment(image, segment);
1478 +               break;
1479 +       }
1480 +       return result;
1481 +}
1482 +
1483 +/*
1484 + * Exec Kernel system call: for obvious reasons only root may call it.
1485 + *
1486 + * This call breaks up into three pieces.
1487 + * - A generic part which loads the new kernel from the current
1488 + *   address space, and very carefully places the data in the
1489 + *   allocated pages.
1490 + *
1491 + * - A generic part that interacts with the kernel and tells all of
1492 + *   the devices to shut down.  Preventing on-going dmas, and placing
1493 + *   the devices in a consistent state so a later kernel can
1494 + *   reinitialize them.
1495 + *
1496 + * - A machine specific part that includes the syscall number
1497 + *   and the copies the image to it's final destination.  And
1498 + *   jumps into the image at entry.
1499 + *
1500 + * kexec does not sync, or unmount filesystems so if you need
1501 + * that to happen you need to do that yourself.
1502 + */
1503 +struct kimage *kexec_image = NULL;
1504 +struct kimage *kexec_crash_image = NULL;
1505 +/*
1506 + * A home grown binary mutex.
1507 + * Nothing can wait so this mutex is safe to use
1508 + * in interrupt context :)
1509 + */
1510 +static int kexec_lock = 0;
1511 +
1512 +asmlinkage long sys_kexec_load(unsigned long entry,
1513 +       unsigned long nr_segments, struct kexec_segment __user *segments,
1514 +       unsigned long flags)
1515 +{
1516 +       struct kimage **dest_image, *image;
1517 +       int locked;
1518 +       int result;
1519 +
1520 +       /* We only trust the superuser with rebooting the system. */
1521 +       if (!capable(CAP_SYS_BOOT))
1522 +               return -EPERM;
1523 +
1524 +       /*
1525 +        * Verify we have a legal set of flags
1526 +        * This leaves us room for future extensions.
1527 +        */
1528 +       if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
1529 +               return -EINVAL;
1530 +
1531 +       /* Verify we are on the appropriate architecture */
1532 +       if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
1533 +               ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
1534 +       {
1535 +               return -EINVAL;
1536 +       }
1537 +
1538 +       /* Put an artificial cap on the number
1539 +        * of segments passed to kexec_load.
1540 +        */
1541 +       if (nr_segments > KEXEC_SEGMENT_MAX)
1542 +               return -EINVAL;
1543 +
1544 +       image = NULL;
1545 +       result = 0;
1546 +
1547 +       /* Because we write directly to the reserved memory
1548 +        * region when loading crash kernels we need a mutex here to
1549 +        * prevent multiple crash  kernels from attempting to load
1550 +        * simultaneously, and to prevent a crash kernel from loading
1551 +        * over the top of a in use crash kernel.
1552 +        *
1553 +        * KISS: always take the mutex.
1554 +        */
1555 +       locked = xchg(&kexec_lock, 1);
1556 +       if (locked) {
1557 +               return -EBUSY;
1558 +       }
1559 +       dest_image = &kexec_image;
1560 +       if (flags & KEXEC_ON_CRASH) {
1561 +               dest_image = &kexec_crash_image;
1562 +       }
1563 +       if (nr_segments > 0) {
1564 +               unsigned long i;
1565 +               /* Loading another kernel to reboot into */
1566 +               if ((flags & KEXEC_ON_CRASH) == 0) {
1567 +                       result = kimage_normal_alloc(&image, entry, nr_segments, segments);
1568 +               }
1569 +               /* Loading another kernel to switch to if this one crashes */
1570 +               else if (flags & KEXEC_ON_CRASH) {
1571 +                       /* Free any current crash dump kernel before
1572 +                        * we corrupt it.
1573 +                        */
1574 +                       kimage_free(xchg(&kexec_crash_image, NULL));
1575 +                       result = kimage_crash_alloc(&image, entry, nr_segments, segments);
1576 +               }
1577 +               if (result) {
1578 +                       goto out;
1579 +               }
1580 +               result = machine_kexec_prepare(image);
1581 +               if (result) {
1582 +                       goto out;
1583 +               }
1584 +               for(i = 0; i < nr_segments; i++) {
1585 +                       result = kimage_load_segment(image, &image->segment[i]);
1586 +                       if (result) {
1587 +                               goto out;
1588 +                       }
1589 +               }
1590 +               result = kimage_terminate(image);
1591 +               if (result) {
1592 +                       goto out;
1593 +               }
1594 +       }
1595 +       /* Install the new kernel, and  Uninstall the old */
1596 +       image = xchg(dest_image, image);
1597 +
1598 + out:
1599 +       xchg(&kexec_lock, 0); /* Release the mutex */
1600 +       kimage_free(image);
1601 +       return result;
1602 +}
1603 +
1604 +void crash_kexec(void)
1605 +{
1606 +       struct kimage *image;
1607 +       int locked;
1608 +
1609 +
1610 +       /* Take the kexec_lock here to prevent sys_kexec_load
1611 +        * running on one cpu from replacing the crash kernel
1612 +        * we are using after a panic on a different cpu.
1613 +        *
1614 +        * If the crash kernel was not located in a fixed area
1615 +        * of memory the xchg(&kexec_crash_image) would be
1616 +        * sufficient.  But since I reuse the memory...
1617 +        */
1618 +       locked = xchg(&kexec_lock, 1);
1619 +       if (!locked) {
1620 +               image = xchg(&kexec_crash_image, NULL);
1621 +               if (image) {
1622 +                       machine_crash_shutdown();
1623 +                       machine_kexec(image);
1624 +               }
1625 +               xchg(&kexec_lock, 0);
1626 +       }
1627 +}
1628 diff -Narup linux-2.4.31-orig/kernel/panic.c linux-2.4.31/kernel/panic.c
1629 --- linux-2.4.31-orig/kernel/panic.c    2004-11-17 03:54:22.000000000 -0800
1630 +++ linux-2.4.31/kernel/panic.c 2005-08-24 22:40:47.000000000 -0700
1631 @@ -17,6 +17,7 @@
1632  #include <linux/sysrq.h>
1633  #include <linux/interrupt.h>
1634  #include <linux/console.h>
1635 +#include <linux/kexec.h>
1636  
1637  asmlinkage void sys_sync(void);        /* it's really int */
1638  
1639 @@ -70,6 +71,11 @@ NORET_TYPE void panic(const char * fmt, 
1640                 sys_sync();
1641         bust_spinlocks(0);
1642  
1643 +       /* if we crash and have a crash kernel loaded
1644 +        * let it handle everything else
1645 +        */
1646 +        crash_kexec();
1647 +        
1648  #ifdef CONFIG_SMP
1649         smp_send_stop();
1650  #endif
1651 diff -Narup linux-2.4.31-orig/kernel/sys.c linux-2.4.31/kernel/sys.c
1652 --- linux-2.4.31-orig/kernel/sys.c      2003-11-28 10:26:21.000000000 -0800
1653 +++ linux-2.4.31/kernel/sys.c   2005-08-24 23:01:24.000000000 -0700
1654 @@ -15,6 +15,9 @@
1655  #include <linux/init.h>
1656  #include <linux/highuid.h>
1657  
1658 +#include <linux/kernel.h>
1659 +#include <linux/kexec.h>
1660 +
1661  #include <asm/uaccess.h>
1662  #include <asm/io.h>
1663  
1664 @@ -342,6 +345,21 @@ asmlinkage long sys_reboot(int magic1, i
1665                 machine_restart(buffer);
1666                 break;
1667  
1668 +       case LINUX_REBOOT_CMD_KEXEC:
1669 +       {
1670 +               struct kimage *image;
1671 +               image = xchg(&kexec_image, 0);
1672 +               if (!image) {
1673 +                       unlock_kernel();
1674 +                       return -EINVAL;
1675 +               }
1676 +               notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
1677 +               printk(KERN_EMERG "Starting new kernel\n");
1678 +               machine_shutdown();
1679 +               machine_kexec(image);
1680 +               break;
1681 +       }
1682 +
1683         default:
1684                 unlock_kernel();
1685                 return -EINVAL;