Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * arch/alpha/boot/bootpz.c
  3 *
  4 * Copyright (C) 1997 Jay Estabrook
  5 *
  6 * This file is used for creating a compressed BOOTP file for the
  7 * Linux/AXP kernel
  8 *
  9 * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
 10 * and the decompression code from MILO.
 11 */
 12#include <linux/kernel.h>
 13#include <linux/slab.h>
 14#include <linux/string.h>
 15#include <generated/utsrelease.h>
 16#include <linux/mm.h>
 17
 18#include <asm/console.h>
 19#include <asm/hwrpb.h>
 20#include <asm/pgtable.h>
 21#include <asm/io.h>
 22
 23#include <stdarg.h>
 24
 25#include "kzsize.h"
 26
 27/* FIXME FIXME FIXME */
 28#define MALLOC_AREA_SIZE 0x200000 /* 2MB for now */
 29/* FIXME FIXME FIXME */
 30
 31
 32/*
 33  WARNING NOTE
 34
 35  It is very possible that turning on additional messages may cause
 36  kernel image corruption due to stack usage to do the printing.
 37
 38*/
 39
 40#undef DEBUG_CHECK_RANGE
 41#undef DEBUG_ADDRESSES
 42#undef DEBUG_LAST_STEPS
 43
 44extern unsigned long switch_to_osf_pal(unsigned long nr,
 45	struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
 46	unsigned long *vptb);
 47
 48extern int decompress_kernel(void* destination, void *source,
 49			     size_t ksize, size_t kzsize);
 50
 51extern void move_stack(unsigned long new_stack);
 52
 53struct hwrpb_struct *hwrpb = INIT_HWRPB;
 54static struct pcb_struct pcb_va[1];
 55
 56/*
 57 * Find a physical address of a virtual object..
 58 *
 59 * This is easy using the virtual page table address.
 60 */
 61#define VPTB	((unsigned long *) 0x200000000)
 62
 63static inline unsigned long
 64find_pa(unsigned long address)
 65{
 66	unsigned long result;
 67
 68	result = VPTB[address >> 13];
 69	result >>= 32;
 70	result <<= 13;
 71	result |= address & 0x1fff;
 72	return result;
 73}	
 74
 75int
 76check_range(unsigned long vstart, unsigned long vend,
 77	    unsigned long kstart, unsigned long kend)
 78{
 79	unsigned long vaddr, kaddr;
 80
 81#ifdef DEBUG_CHECK_RANGE
 82	srm_printk("check_range: V[0x%lx:0x%lx] K[0x%lx:0x%lx]\n",
 83		   vstart, vend, kstart, kend);
 84#endif
 85	/* do some range checking for detecting an overlap... */
 86	for (vaddr = vstart; vaddr <= vend; vaddr += PAGE_SIZE)
 87	{
 88		kaddr = (find_pa(vaddr) | PAGE_OFFSET);
 89		if (kaddr >= kstart && kaddr <= kend)
 90		{
 91#ifdef DEBUG_CHECK_RANGE
 92			srm_printk("OVERLAP: vaddr 0x%lx kaddr 0x%lx"
 93				   " [0x%lx:0x%lx]\n",
 94				   vaddr, kaddr, kstart, kend);
 95#endif
 96			return 1;
 97		}
 98	}
 99	return 0;
100}
101
102/*
103 * This function moves into OSF/1 pal-code, and has a temporary
104 * PCB for that. The kernel proper should replace this PCB with
105 * the real one as soon as possible.
106 *
107 * The page table muckery in here depends on the fact that the boot
108 * code has the L1 page table identity-map itself in the second PTE
109 * in the L1 page table. Thus the L1-page is virtually addressable
110 * itself (through three levels) at virtual address 0x200802000.
111 */
112
113#define L1	((unsigned long *) 0x200802000)
114
115void
116pal_init(void)
117{
118	unsigned long i, rev;
119	struct percpu_struct * percpu;
120	struct pcb_struct * pcb_pa;
121
122	/* Create the dummy PCB.  */
123	pcb_va->ksp = 0;
124	pcb_va->usp = 0;
125	pcb_va->ptbr = L1[1] >> 32;
126	pcb_va->asn = 0;
127	pcb_va->pcc = 0;
128	pcb_va->unique = 0;
129	pcb_va->flags = 1;
130	pcb_va->res1 = 0;
131	pcb_va->res2 = 0;
132	pcb_pa = (struct pcb_struct *)find_pa((unsigned long)pcb_va);
133
134	/*
135	 * a0 = 2 (OSF)
136	 * a1 = return address, but we give the asm the vaddr of the PCB
137	 * a2 = physical addr of PCB
138	 * a3 = new virtual page table pointer
139	 * a4 = KSP (but the asm sets it)
140	 */
141	srm_printk("Switching to OSF PAL-code... ");
142
143	i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
144	if (i) {
145		srm_printk("failed, code %ld\n", i);
146		__halt();
147	}
148
149	percpu = (struct percpu_struct *)
150		(INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
151	rev = percpu->pal_revision = percpu->palcode_avail[2];
152
153	srm_printk("OK (rev %lx)\n", rev);
154
155	tbia(); /* do it directly in case we are SMP */
156}
157
158/*
159 * Start the kernel.
160 */
161static inline void
162runkernel(void)
163{
164	__asm__ __volatile__(
165		"bis %0,%0,$27\n\t"
166		"jmp ($27)"
167		: /* no outputs: it doesn't even return */
168		: "r" (START_ADDR));
169}
170
171/* Must record the SP (it is virtual) on entry, so we can make sure
172   not to overwrite it during movement or decompression. */
173unsigned long SP_on_entry;
174
175/* Calculate the kernel image address based on the end of the BOOTP
176   bootstrapper (ie this program).
177*/
178extern char _end;
179#define KERNEL_ORIGIN \
180	((((unsigned long)&_end) + 511) & ~511)
181
182/* Round address to next higher page boundary. */
183#define NEXT_PAGE(a)	(((a) | (PAGE_SIZE - 1)) + 1)
184
185#ifdef INITRD_IMAGE_SIZE
186# define REAL_INITRD_SIZE INITRD_IMAGE_SIZE
187#else
188# define REAL_INITRD_SIZE 0
189#endif
190
191/* Defines from include/asm-alpha/system.h
192
193	BOOT_ADDR	Virtual address at which the consoles loads
194			the BOOTP image.
195
196	KERNEL_START    KSEG address at which the kernel is built to run,
197			which includes some initial data pages before the
198			code.
199
200	START_ADDR	KSEG address of the entry point of kernel code.
201
202	ZERO_PGE	KSEG address of page full of zeroes, but 
203			upon entry to kerne cvan be expected
204			to hold the parameter list and possible
205			INTRD information.
206
207   These are used in the local defines below.
208*/
209  
210
211/* Virtual addresses for the BOOTP image. Note that this includes the
212   bootstrapper code as well as the compressed kernel image, and
213   possibly the INITRD image.
214
215   Oh, and do NOT forget the STACK, which appears to be placed virtually
216   beyond the end of the loaded image.
217*/
218#define V_BOOT_IMAGE_START	BOOT_ADDR
219#define V_BOOT_IMAGE_END	SP_on_entry
220
221/* Virtual addresses for just the bootstrapper part of the BOOTP image. */
222#define V_BOOTSTRAPPER_START	BOOT_ADDR
223#define V_BOOTSTRAPPER_END	KERNEL_ORIGIN
224
225/* Virtual addresses for just the data part of the BOOTP
226   image. This may also include the INITRD image, but always
227   includes the STACK.
228*/
229#define V_DATA_START		KERNEL_ORIGIN
230#define V_INITRD_START		(KERNEL_ORIGIN + KERNEL_Z_SIZE)
231#define V_INTRD_END		(V_INITRD_START + REAL_INITRD_SIZE)
232#define V_DATA_END	 	V_BOOT_IMAGE_END
233
234/* KSEG addresses for the uncompressed kernel.
235
236   Note that the end address includes workspace for the decompression.
237   Note also that the DATA_START address is ZERO_PGE, to which we write
238   just before jumping to the kernel image at START_ADDR.
239 */
240#define K_KERNEL_DATA_START	ZERO_PGE
241#define K_KERNEL_IMAGE_START	START_ADDR
242#define K_KERNEL_IMAGE_END	(START_ADDR + KERNEL_SIZE)
243
244/* Define to where we may have to decompress the kernel image, before
245   we move it to the final position, in case of overlap. This will be
246   above the final position of the kernel.
247
248   Regardless of overlap, we move the INITRD image to the end of this
249   copy area, because there needs to be a buffer area after the kernel
250   for "bootmem" anyway.
251*/
252#define K_COPY_IMAGE_START	NEXT_PAGE(K_KERNEL_IMAGE_END)
253/* Reserve one page below INITRD for the new stack. */
254#define K_INITRD_START \
255    NEXT_PAGE(K_COPY_IMAGE_START + KERNEL_SIZE + PAGE_SIZE)
256#define K_COPY_IMAGE_END \
257    (K_INITRD_START + REAL_INITRD_SIZE + MALLOC_AREA_SIZE)
258#define K_COPY_IMAGE_SIZE \
259    NEXT_PAGE(K_COPY_IMAGE_END - K_COPY_IMAGE_START)
260
261void
262start_kernel(void)
263{
264	int must_move = 0;
265
266	/* Initialize these for the decompression-in-place situation,
267	   which is the smallest amount of work and most likely to
268	   occur when using the normal START_ADDR of the kernel
269	   (currently set to 16MB, to clear all console code.
270	*/
271	unsigned long uncompressed_image_start = K_KERNEL_IMAGE_START;
272	unsigned long uncompressed_image_end = K_KERNEL_IMAGE_END;
273
274	unsigned long initrd_image_start = K_INITRD_START;
275
276	/*
277	 * Note that this crufty stuff with static and envval
278	 * and envbuf is because:
279	 *
280	 * 1. Frequently, the stack is short, and we don't want to overrun;
281	 * 2. Frequently the stack is where we are going to copy the kernel to;
282	 * 3. A certain SRM console required the GET_ENV output to stack.
283	 *    ??? A comment in the aboot sources indicates that the GET_ENV
284	 *    destination must be quadword aligned.  Might this explain the
285	 *    behaviour, rather than requiring output to the stack, which
286	 *    seems rather far-fetched.
287	 */
288	static long nbytes;
289	static char envval[256] __attribute__((aligned(8)));
290	register unsigned long asm_sp asm("30");
291
292	SP_on_entry = asm_sp;
293
294	srm_printk("Linux/Alpha BOOTPZ Loader for Linux " UTS_RELEASE "\n");
295
296	/* Validity check the HWRPB. */
297	if (INIT_HWRPB->pagesize != 8192) {
298		srm_printk("Expected 8kB pages, got %ldkB\n",
299		           INIT_HWRPB->pagesize >> 10);
300		return;
301	}
302	if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
303		srm_printk("Expected vptb at %p, got %p\n",
304			   VPTB, (void *)INIT_HWRPB->vptb);
305		return;
306	}
307
308	/* PALcode (re)initialization. */
309	pal_init();
310
311	/* Get the parameter list from the console environment variable. */
312	nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
313	if (nbytes < 0 || nbytes >= sizeof(envval)) {
314		nbytes = 0;
315	}
316	envval[nbytes] = '\0';
317
318#ifdef DEBUG_ADDRESSES
319	srm_printk("START_ADDR 0x%lx\n", START_ADDR);
320	srm_printk("KERNEL_ORIGIN 0x%lx\n", KERNEL_ORIGIN);
321	srm_printk("KERNEL_SIZE 0x%x\n", KERNEL_SIZE);
322	srm_printk("KERNEL_Z_SIZE 0x%x\n", KERNEL_Z_SIZE);
323#endif
324
325	/* Since all the SRM consoles load the BOOTP image at virtual
326	 * 0x20000000, we have to ensure that the physical memory
327	 * pages occupied by that image do NOT overlap the physical
328	 * address range where the kernel wants to be run.  This
329	 * causes real problems when attempting to cdecompress the
330	 * former into the latter... :-(
331	 *
332	 * So, we may have to decompress/move the kernel/INITRD image
333	 * virtual-to-physical someplace else first before moving
334	 * kernel /INITRD to their final resting places... ;-}
335	 *
336	 * Sigh...
337	 */
338
339	/* First, check to see if the range of addresses occupied by
340	   the bootstrapper part of the BOOTP image include any of the
341	   physical pages into which the kernel will be placed for
342	   execution.
343
344	   We only need check on the final kernel image range, since we
345	   will put the INITRD someplace that we can be sure is not
346	   in conflict.
347	 */
348	if (check_range(V_BOOTSTRAPPER_START, V_BOOTSTRAPPER_END,
349			K_KERNEL_DATA_START, K_KERNEL_IMAGE_END))
350	{
351		srm_printk("FATAL ERROR: overlap of bootstrapper code\n");
352		__halt();
353	}
354
355	/* Next, check to see if the range of addresses occupied by
356	   the compressed kernel/INITRD/stack portion of the BOOTP
357	   image include any of the physical pages into which the
358	   decompressed kernel or the INITRD will be placed for
359	   execution.
360	 */
361	if (check_range(V_DATA_START, V_DATA_END,
362			K_KERNEL_IMAGE_START, K_COPY_IMAGE_END))
363	{
364#ifdef DEBUG_ADDRESSES
365		srm_printk("OVERLAP: cannot decompress in place\n");
366#endif
367		uncompressed_image_start = K_COPY_IMAGE_START;
368		uncompressed_image_end = K_COPY_IMAGE_END;
369		must_move = 1;
370
371		/* Finally, check to see if the range of addresses
372		   occupied by the compressed kernel/INITRD part of
373		   the BOOTP image include any of the physical pages
374		   into which that part is to be copied for
375		   decompression.
376		*/
377		while (check_range(V_DATA_START, V_DATA_END,
378				   uncompressed_image_start,
379				   uncompressed_image_end))
380		{
381#if 0
382			uncompressed_image_start += K_COPY_IMAGE_SIZE;
383			uncompressed_image_end += K_COPY_IMAGE_SIZE;
384			initrd_image_start += K_COPY_IMAGE_SIZE;
385#else
386			/* Keep as close as possible to end of BOOTP image. */
387			uncompressed_image_start += PAGE_SIZE;
388			uncompressed_image_end += PAGE_SIZE;
389			initrd_image_start += PAGE_SIZE;
390#endif
391		}
392	}
393
394	srm_printk("Starting to load the kernel with args '%s'\n", envval);
395
396#ifdef DEBUG_ADDRESSES
397	srm_printk("Decompressing the kernel...\n"
398		   "...from 0x%lx to 0x%lx size 0x%x\n",
399		   V_DATA_START,
400		   uncompressed_image_start,
401		   KERNEL_SIZE);
402#endif
403        decompress_kernel((void *)uncompressed_image_start,
404			  (void *)V_DATA_START,
405			  KERNEL_SIZE, KERNEL_Z_SIZE);
406
407	/*
408	 * Now, move things to their final positions, if/as required.
409	 */
410
411#ifdef INITRD_IMAGE_SIZE
412
413	/* First, we always move the INITRD image, if present. */
414#ifdef DEBUG_ADDRESSES
415	srm_printk("Moving the INITRD image...\n"
416		   " from 0x%lx to 0x%lx size 0x%x\n",
417		   V_INITRD_START,
418		   initrd_image_start,
419		   INITRD_IMAGE_SIZE);
420#endif
421	memcpy((void *)initrd_image_start, (void *)V_INITRD_START,
422	       INITRD_IMAGE_SIZE);
423
424#endif /* INITRD_IMAGE_SIZE */
425
426	/* Next, we may have to move the uncompressed kernel to the
427	   final destination.
428	 */
429	if (must_move) {
430#ifdef DEBUG_ADDRESSES
431		srm_printk("Moving the uncompressed kernel...\n"
432			   "...from 0x%lx to 0x%lx size 0x%x\n",
433			   uncompressed_image_start,
434			   K_KERNEL_IMAGE_START,
435			   (unsigned)KERNEL_SIZE);
436#endif
437		/*
438		 * Move the stack to a safe place to ensure it won't be
439		 * overwritten by kernel image.
440		 */
441		move_stack(initrd_image_start - PAGE_SIZE);
442
443		memcpy((void *)K_KERNEL_IMAGE_START,
444		       (void *)uncompressed_image_start, KERNEL_SIZE);
445	}
446	
447	/* Clear the zero page, then move the argument list in. */
448#ifdef DEBUG_LAST_STEPS
449	srm_printk("Preparing ZERO_PGE...\n");
450#endif
451	memset((char*)ZERO_PGE, 0, PAGE_SIZE);
452	strcpy((char*)ZERO_PGE, envval);
453
454#ifdef INITRD_IMAGE_SIZE
455
456#ifdef DEBUG_LAST_STEPS
457	srm_printk("Preparing INITRD info...\n");
458#endif
459	/* Finally, set the INITRD paramenters for the kernel. */
460	((long *)(ZERO_PGE+256))[0] = initrd_image_start;
461	((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;
462
463#endif /* INITRD_IMAGE_SIZE */
464
465#ifdef DEBUG_LAST_STEPS
466	srm_printk("Doing 'runkernel()'...\n");
467#endif
468	runkernel();
469}
470
471 /* dummy function, should never be called. */
472void *__kmalloc(size_t size, gfp_t flags)
473{
474	return (void *)NULL;
475}
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * arch/alpha/boot/bootpz.c
  4 *
  5 * Copyright (C) 1997 Jay Estabrook
  6 *
  7 * This file is used for creating a compressed BOOTP file for the
  8 * Linux/AXP kernel
  9 *
 10 * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
 11 * and the decompression code from MILO.
 12 */
 13#include <linux/kernel.h>
 14#include <linux/slab.h>
 15#include <linux/string.h>
 16#include <generated/utsrelease.h>
 17#include <linux/mm.h>
 18
 19#include <asm/console.h>
 20#include <asm/hwrpb.h>
 21#include <asm/pgtable.h>
 22#include <asm/io.h>
 23
 24#include <stdarg.h>
 25
 26#include "kzsize.h"
 27
 28/* FIXME FIXME FIXME */
 29#define MALLOC_AREA_SIZE 0x200000 /* 2MB for now */
 30/* FIXME FIXME FIXME */
 31
 32
 33/*
 34  WARNING NOTE
 35
 36  It is very possible that turning on additional messages may cause
 37  kernel image corruption due to stack usage to do the printing.
 38
 39*/
 40
 41#undef DEBUG_CHECK_RANGE
 42#undef DEBUG_ADDRESSES
 43#undef DEBUG_LAST_STEPS
 44
 45extern unsigned long switch_to_osf_pal(unsigned long nr,
 46	struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
 47	unsigned long *vptb);
 48
 49extern int decompress_kernel(void* destination, void *source,
 50			     size_t ksize, size_t kzsize);
 51
 52extern void move_stack(unsigned long new_stack);
 53
 54struct hwrpb_struct *hwrpb = INIT_HWRPB;
 55static struct pcb_struct pcb_va[1];
 56
 57/*
 58 * Find a physical address of a virtual object..
 59 *
 60 * This is easy using the virtual page table address.
 61 */
 62#define VPTB	((unsigned long *) 0x200000000)
 63
 64static inline unsigned long
 65find_pa(unsigned long address)
 66{
 67	unsigned long result;
 68
 69	result = VPTB[address >> 13];
 70	result >>= 32;
 71	result <<= 13;
 72	result |= address & 0x1fff;
 73	return result;
 74}	
 75
 76int
 77check_range(unsigned long vstart, unsigned long vend,
 78	    unsigned long kstart, unsigned long kend)
 79{
 80	unsigned long vaddr, kaddr;
 81
 82#ifdef DEBUG_CHECK_RANGE
 83	srm_printk("check_range: V[0x%lx:0x%lx] K[0x%lx:0x%lx]\n",
 84		   vstart, vend, kstart, kend);
 85#endif
 86	/* do some range checking for detecting an overlap... */
 87	for (vaddr = vstart; vaddr <= vend; vaddr += PAGE_SIZE)
 88	{
 89		kaddr = (find_pa(vaddr) | PAGE_OFFSET);
 90		if (kaddr >= kstart && kaddr <= kend)
 91		{
 92#ifdef DEBUG_CHECK_RANGE
 93			srm_printk("OVERLAP: vaddr 0x%lx kaddr 0x%lx"
 94				   " [0x%lx:0x%lx]\n",
 95				   vaddr, kaddr, kstart, kend);
 96#endif
 97			return 1;
 98		}
 99	}
100	return 0;
101}
102
103/*
104 * This function moves into OSF/1 pal-code, and has a temporary
105 * PCB for that. The kernel proper should replace this PCB with
106 * the real one as soon as possible.
107 *
108 * The page table muckery in here depends on the fact that the boot
109 * code has the L1 page table identity-map itself in the second PTE
110 * in the L1 page table. Thus the L1-page is virtually addressable
111 * itself (through three levels) at virtual address 0x200802000.
112 */
113
114#define L1	((unsigned long *) 0x200802000)
115
116void
117pal_init(void)
118{
119	unsigned long i, rev;
120	struct percpu_struct * percpu;
121	struct pcb_struct * pcb_pa;
122
123	/* Create the dummy PCB.  */
124	pcb_va->ksp = 0;
125	pcb_va->usp = 0;
126	pcb_va->ptbr = L1[1] >> 32;
127	pcb_va->asn = 0;
128	pcb_va->pcc = 0;
129	pcb_va->unique = 0;
130	pcb_va->flags = 1;
131	pcb_va->res1 = 0;
132	pcb_va->res2 = 0;
133	pcb_pa = (struct pcb_struct *)find_pa((unsigned long)pcb_va);
134
135	/*
136	 * a0 = 2 (OSF)
137	 * a1 = return address, but we give the asm the vaddr of the PCB
138	 * a2 = physical addr of PCB
139	 * a3 = new virtual page table pointer
140	 * a4 = KSP (but the asm sets it)
141	 */
142	srm_printk("Switching to OSF PAL-code... ");
143
144	i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
145	if (i) {
146		srm_printk("failed, code %ld\n", i);
147		__halt();
148	}
149
150	percpu = (struct percpu_struct *)
151		(INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
152	rev = percpu->pal_revision = percpu->palcode_avail[2];
153
154	srm_printk("OK (rev %lx)\n", rev);
155
156	tbia(); /* do it directly in case we are SMP */
157}
158
159/*
160 * Start the kernel.
161 */
162static inline void
163runkernel(void)
164{
165	__asm__ __volatile__(
166		"bis %0,%0,$27\n\t"
167		"jmp ($27)"
168		: /* no outputs: it doesn't even return */
169		: "r" (START_ADDR));
170}
171
172/* Must record the SP (it is virtual) on entry, so we can make sure
173   not to overwrite it during movement or decompression. */
174unsigned long SP_on_entry;
175
176/* Calculate the kernel image address based on the end of the BOOTP
177   bootstrapper (ie this program).
178*/
179extern char _end;
180#define KERNEL_ORIGIN \
181	((((unsigned long)&_end) + 511) & ~511)
182
183/* Round address to next higher page boundary. */
184#define NEXT_PAGE(a)	(((a) | (PAGE_SIZE - 1)) + 1)
185
186#ifdef INITRD_IMAGE_SIZE
187# define REAL_INITRD_SIZE INITRD_IMAGE_SIZE
188#else
189# define REAL_INITRD_SIZE 0
190#endif
191
192/* Defines from include/asm-alpha/system.h
193
194	BOOT_ADDR	Virtual address at which the consoles loads
195			the BOOTP image.
196
197	KERNEL_START    KSEG address at which the kernel is built to run,
198			which includes some initial data pages before the
199			code.
200
201	START_ADDR	KSEG address of the entry point of kernel code.
202
203	ZERO_PGE	KSEG address of page full of zeroes, but 
204			upon entry to kerne cvan be expected
205			to hold the parameter list and possible
206			INTRD information.
207
208   These are used in the local defines below.
209*/
210  
211
212/* Virtual addresses for the BOOTP image. Note that this includes the
213   bootstrapper code as well as the compressed kernel image, and
214   possibly the INITRD image.
215
216   Oh, and do NOT forget the STACK, which appears to be placed virtually
217   beyond the end of the loaded image.
218*/
219#define V_BOOT_IMAGE_START	BOOT_ADDR
220#define V_BOOT_IMAGE_END	SP_on_entry
221
222/* Virtual addresses for just the bootstrapper part of the BOOTP image. */
223#define V_BOOTSTRAPPER_START	BOOT_ADDR
224#define V_BOOTSTRAPPER_END	KERNEL_ORIGIN
225
226/* Virtual addresses for just the data part of the BOOTP
227   image. This may also include the INITRD image, but always
228   includes the STACK.
229*/
230#define V_DATA_START		KERNEL_ORIGIN
231#define V_INITRD_START		(KERNEL_ORIGIN + KERNEL_Z_SIZE)
232#define V_INTRD_END		(V_INITRD_START + REAL_INITRD_SIZE)
233#define V_DATA_END	 	V_BOOT_IMAGE_END
234
235/* KSEG addresses for the uncompressed kernel.
236
237   Note that the end address includes workspace for the decompression.
238   Note also that the DATA_START address is ZERO_PGE, to which we write
239   just before jumping to the kernel image at START_ADDR.
240 */
241#define K_KERNEL_DATA_START	ZERO_PGE
242#define K_KERNEL_IMAGE_START	START_ADDR
243#define K_KERNEL_IMAGE_END	(START_ADDR + KERNEL_SIZE)
244
245/* Define to where we may have to decompress the kernel image, before
246   we move it to the final position, in case of overlap. This will be
247   above the final position of the kernel.
248
249   Regardless of overlap, we move the INITRD image to the end of this
250   copy area, because there needs to be a buffer area after the kernel
251   for "bootmem" anyway.
252*/
253#define K_COPY_IMAGE_START	NEXT_PAGE(K_KERNEL_IMAGE_END)
254/* Reserve one page below INITRD for the new stack. */
255#define K_INITRD_START \
256    NEXT_PAGE(K_COPY_IMAGE_START + KERNEL_SIZE + PAGE_SIZE)
257#define K_COPY_IMAGE_END \
258    (K_INITRD_START + REAL_INITRD_SIZE + MALLOC_AREA_SIZE)
259#define K_COPY_IMAGE_SIZE \
260    NEXT_PAGE(K_COPY_IMAGE_END - K_COPY_IMAGE_START)
261
262void
263start_kernel(void)
264{
265	int must_move = 0;
266
267	/* Initialize these for the decompression-in-place situation,
268	   which is the smallest amount of work and most likely to
269	   occur when using the normal START_ADDR of the kernel
270	   (currently set to 16MB, to clear all console code.
271	*/
272	unsigned long uncompressed_image_start = K_KERNEL_IMAGE_START;
273	unsigned long uncompressed_image_end = K_KERNEL_IMAGE_END;
274
275	unsigned long initrd_image_start = K_INITRD_START;
276
277	/*
278	 * Note that this crufty stuff with static and envval
279	 * and envbuf is because:
280	 *
281	 * 1. Frequently, the stack is short, and we don't want to overrun;
282	 * 2. Frequently the stack is where we are going to copy the kernel to;
283	 * 3. A certain SRM console required the GET_ENV output to stack.
284	 *    ??? A comment in the aboot sources indicates that the GET_ENV
285	 *    destination must be quadword aligned.  Might this explain the
286	 *    behaviour, rather than requiring output to the stack, which
287	 *    seems rather far-fetched.
288	 */
289	static long nbytes;
290	static char envval[256] __attribute__((aligned(8)));
291	register unsigned long asm_sp asm("30");
292
293	SP_on_entry = asm_sp;
294
295	srm_printk("Linux/Alpha BOOTPZ Loader for Linux " UTS_RELEASE "\n");
296
297	/* Validity check the HWRPB. */
298	if (INIT_HWRPB->pagesize != 8192) {
299		srm_printk("Expected 8kB pages, got %ldkB\n",
300		           INIT_HWRPB->pagesize >> 10);
301		return;
302	}
303	if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
304		srm_printk("Expected vptb at %p, got %p\n",
305			   VPTB, (void *)INIT_HWRPB->vptb);
306		return;
307	}
308
309	/* PALcode (re)initialization. */
310	pal_init();
311
312	/* Get the parameter list from the console environment variable. */
313	nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
314	if (nbytes < 0 || nbytes >= sizeof(envval)) {
315		nbytes = 0;
316	}
317	envval[nbytes] = '\0';
318
319#ifdef DEBUG_ADDRESSES
320	srm_printk("START_ADDR 0x%lx\n", START_ADDR);
321	srm_printk("KERNEL_ORIGIN 0x%lx\n", KERNEL_ORIGIN);
322	srm_printk("KERNEL_SIZE 0x%x\n", KERNEL_SIZE);
323	srm_printk("KERNEL_Z_SIZE 0x%x\n", KERNEL_Z_SIZE);
324#endif
325
326	/* Since all the SRM consoles load the BOOTP image at virtual
327	 * 0x20000000, we have to ensure that the physical memory
328	 * pages occupied by that image do NOT overlap the physical
329	 * address range where the kernel wants to be run.  This
330	 * causes real problems when attempting to cdecompress the
331	 * former into the latter... :-(
332	 *
333	 * So, we may have to decompress/move the kernel/INITRD image
334	 * virtual-to-physical someplace else first before moving
335	 * kernel /INITRD to their final resting places... ;-}
336	 *
337	 * Sigh...
338	 */
339
340	/* First, check to see if the range of addresses occupied by
341	   the bootstrapper part of the BOOTP image include any of the
342	   physical pages into which the kernel will be placed for
343	   execution.
344
345	   We only need check on the final kernel image range, since we
346	   will put the INITRD someplace that we can be sure is not
347	   in conflict.
348	 */
349	if (check_range(V_BOOTSTRAPPER_START, V_BOOTSTRAPPER_END,
350			K_KERNEL_DATA_START, K_KERNEL_IMAGE_END))
351	{
352		srm_printk("FATAL ERROR: overlap of bootstrapper code\n");
353		__halt();
354	}
355
356	/* Next, check to see if the range of addresses occupied by
357	   the compressed kernel/INITRD/stack portion of the BOOTP
358	   image include any of the physical pages into which the
359	   decompressed kernel or the INITRD will be placed for
360	   execution.
361	 */
362	if (check_range(V_DATA_START, V_DATA_END,
363			K_KERNEL_IMAGE_START, K_COPY_IMAGE_END))
364	{
365#ifdef DEBUG_ADDRESSES
366		srm_printk("OVERLAP: cannot decompress in place\n");
367#endif
368		uncompressed_image_start = K_COPY_IMAGE_START;
369		uncompressed_image_end = K_COPY_IMAGE_END;
370		must_move = 1;
371
372		/* Finally, check to see if the range of addresses
373		   occupied by the compressed kernel/INITRD part of
374		   the BOOTP image include any of the physical pages
375		   into which that part is to be copied for
376		   decompression.
377		*/
378		while (check_range(V_DATA_START, V_DATA_END,
379				   uncompressed_image_start,
380				   uncompressed_image_end))
381		{
382#if 0
383			uncompressed_image_start += K_COPY_IMAGE_SIZE;
384			uncompressed_image_end += K_COPY_IMAGE_SIZE;
385			initrd_image_start += K_COPY_IMAGE_SIZE;
386#else
387			/* Keep as close as possible to end of BOOTP image. */
388			uncompressed_image_start += PAGE_SIZE;
389			uncompressed_image_end += PAGE_SIZE;
390			initrd_image_start += PAGE_SIZE;
391#endif
392		}
393	}
394
395	srm_printk("Starting to load the kernel with args '%s'\n", envval);
396
397#ifdef DEBUG_ADDRESSES
398	srm_printk("Decompressing the kernel...\n"
399		   "...from 0x%lx to 0x%lx size 0x%x\n",
400		   V_DATA_START,
401		   uncompressed_image_start,
402		   KERNEL_SIZE);
403#endif
404        decompress_kernel((void *)uncompressed_image_start,
405			  (void *)V_DATA_START,
406			  KERNEL_SIZE, KERNEL_Z_SIZE);
407
408	/*
409	 * Now, move things to their final positions, if/as required.
410	 */
411
412#ifdef INITRD_IMAGE_SIZE
413
414	/* First, we always move the INITRD image, if present. */
415#ifdef DEBUG_ADDRESSES
416	srm_printk("Moving the INITRD image...\n"
417		   " from 0x%lx to 0x%lx size 0x%x\n",
418		   V_INITRD_START,
419		   initrd_image_start,
420		   INITRD_IMAGE_SIZE);
421#endif
422	memcpy((void *)initrd_image_start, (void *)V_INITRD_START,
423	       INITRD_IMAGE_SIZE);
424
425#endif /* INITRD_IMAGE_SIZE */
426
427	/* Next, we may have to move the uncompressed kernel to the
428	   final destination.
429	 */
430	if (must_move) {
431#ifdef DEBUG_ADDRESSES
432		srm_printk("Moving the uncompressed kernel...\n"
433			   "...from 0x%lx to 0x%lx size 0x%x\n",
434			   uncompressed_image_start,
435			   K_KERNEL_IMAGE_START,
436			   (unsigned)KERNEL_SIZE);
437#endif
438		/*
439		 * Move the stack to a safe place to ensure it won't be
440		 * overwritten by kernel image.
441		 */
442		move_stack(initrd_image_start - PAGE_SIZE);
443
444		memcpy((void *)K_KERNEL_IMAGE_START,
445		       (void *)uncompressed_image_start, KERNEL_SIZE);
446	}
447	
448	/* Clear the zero page, then move the argument list in. */
449#ifdef DEBUG_LAST_STEPS
450	srm_printk("Preparing ZERO_PGE...\n");
451#endif
452	memset((char*)ZERO_PGE, 0, PAGE_SIZE);
453	strcpy((char*)ZERO_PGE, envval);
454
455#ifdef INITRD_IMAGE_SIZE
456
457#ifdef DEBUG_LAST_STEPS
458	srm_printk("Preparing INITRD info...\n");
459#endif
460	/* Finally, set the INITRD paramenters for the kernel. */
461	((long *)(ZERO_PGE+256))[0] = initrd_image_start;
462	((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;
463
464#endif /* INITRD_IMAGE_SIZE */
465
466#ifdef DEBUG_LAST_STEPS
467	srm_printk("Doing 'runkernel()'...\n");
468#endif
469	runkernel();
470}
471
472 /* dummy function, should never be called. */
473void *__kmalloc(size_t size, gfp_t flags)
474{
475	return (void *)NULL;
476}