File: /usr/src/linux/arch/alpha/boot/bootp.c

1     /*
2      * arch/alpha/boot/bootp.c
3      *
4      * Copyright (C) 1997 Jay Estabrook
5      *
6      * This file is used for creating a bootp file for the Linux/AXP kernel
7      *
8      * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
9      */
10     #include <linux/kernel.h>
11     #include <linux/string.h>
12     #include <linux/version.h>
13     #include <linux/mm.h>
14     
15     #include <asm/system.h>
16     #include <asm/console.h>
17     #include <asm/hwrpb.h>
18     #include <asm/pgtable.h>
19     #include <asm/io.h>
20     
21     #include <stdarg.h>
22     
23     #include "ksize.h"
24     
25     extern unsigned long switch_to_osf_pal(unsigned long nr,
26     	struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
27     	unsigned long *vptb);
28     
29     struct hwrpb_struct *hwrpb = INIT_HWRPB;
30     static struct pcb_struct pcb_va[1];
31     
32     /*
33      * Find a physical address of a virtual object..
34      *
35      * This is easy using the virtual page table address.
36      */
37     
38     static inline void *
39     find_pa(unsigned long *vptb, void *ptr)
40     {
41     	unsigned long address = (unsigned long) ptr;
42     	unsigned long result;
43     
44     	result = vptb[address >> 13];
45     	result >>= 32;
46     	result <<= 13;
47     	result |= address & 0x1fff;
48     	return (void *) result;
49     }	
50     
51     /*
52      * This function moves into OSF/1 pal-code, and has a temporary
53      * PCB for that. The kernel proper should replace this PCB with
54      * the real one as soon as possible.
55      *
56      * The page table muckery in here depends on the fact that the boot
57      * code has the L1 page table identity-map itself in the second PTE
58      * in the L1 page table. Thus the L1-page is virtually addressable
59      * itself (through three levels) at virtual address 0x200802000.
60      */
61     
62     #define VPTB	((unsigned long *) 0x200000000)
63     #define L1	((unsigned long *) 0x200802000)
64     
65     void
66     pal_init(void)
67     {
68     	unsigned long i, rev;
69     	struct percpu_struct * percpu;
70     	struct pcb_struct * pcb_pa;
71     
72     	/* Create the dummy PCB.  */
73     	pcb_va->ksp = 0;
74     	pcb_va->usp = 0;
75     	pcb_va->ptbr = L1[1] >> 32;
76     	pcb_va->asn = 0;
77     	pcb_va->pcc = 0;
78     	pcb_va->unique = 0;
79     	pcb_va->flags = 1;
80     	pcb_va->res1 = 0;
81     	pcb_va->res2 = 0;
82     	pcb_pa = find_pa(VPTB, pcb_va);
83     
84     	/*
85     	 * a0 = 2 (OSF)
86     	 * a1 = return address, but we give the asm the vaddr of the PCB
87     	 * a2 = physical addr of PCB
88     	 * a3 = new virtual page table pointer
89     	 * a4 = KSP (but the asm sets it)
90     	 */
91     	srm_printk("Switching to OSF PAL-code .. ");
92     
93     	i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
94     	if (i) {
95     		srm_printk("failed, code %ld\n", i);
96     		__halt();
97     	}
98     
99     	percpu = (struct percpu_struct *)
100     		(INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
101     	rev = percpu->pal_revision = percpu->palcode_avail[2];
102     
103     	srm_printk("Ok (rev %lx)\n", rev);
104     
105     	tbia(); /* do it directly in case we are SMP */
106     }
107     
108     static inline void
109     load(unsigned long dst, unsigned long src, unsigned long count)
110     {
111     	memcpy((void *)dst, (void *)src, count);
112     }
113     
114     /*
115      * Start the kernel.
116      */
117     static inline void
118     runkernel(void)
119     {
120     	__asm__ __volatile__(
121     		"bis %1,%1,$30\n\t"
122     		"bis %0,%0,$27\n\t"
123     		"jmp ($27)"
124     		: /* no outputs: it doesn't even return */
125     		: "r" (START_ADDR),
126     		  "r" (PAGE_SIZE + INIT_STACK));
127     }
128     
129     extern char _end;
130     #define KERNEL_ORIGIN \
131     	((((unsigned long)&_end) + 511) & ~511)
132     
133     void
134     start_kernel(void)
135     {
136     	/*
137     	 * Note that this crufty stuff with static and envval
138     	 * and envbuf is because:
139     	 *
140     	 * 1. Frequently, the stack is short, and we don't want to overrun;
141     	 * 2. Frequently the stack is where we are going to copy the kernel to;
142     	 * 3. A certain SRM console required the GET_ENV output to stack.
143     	 *    ??? A comment in the aboot sources indicates that the GET_ENV
144     	 *    destination must be quadword aligned.  Might this explain the
145     	 *    behaviour, rather than requiring output to the stack, which
146     	 *    seems rather far-fetched.
147     	 */
148     	static long nbytes;
149     	static char envval[256] __attribute__((aligned(8)));
150     #ifdef INITRD_SIZE
151     	static unsigned long initrd_start;
152     #endif
153     
154     	srm_printk("Linux/AXP bootp loader for Linux " UTS_RELEASE "\n");
155     	if (INIT_HWRPB->pagesize != 8192) {
156     		srm_printk("Expected 8kB pages, got %ldkB\n",
157     		           INIT_HWRPB->pagesize >> 10);
158     		return;
159     	}
160     	if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
161     		srm_printk("Expected vptb at %p, got %p\n",
162     			   VPTB, (void *)INIT_HWRPB->vptb);
163     		return;
164     	}
165     	pal_init();
166     
167     #ifdef INITRD_SIZE
168     	/* The initrd must be page-aligned.  See below for the 
169     	   cause of the magic number 5.  */
170     	initrd_start = ((START_ADDR + 5*KERNEL_SIZE) | (PAGE_SIZE-1)) + 1;
171     	srm_printk("Initrd positioned at %#lx\n", initrd_start);
172     #endif
173     
174     	nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
175     	if (nbytes < 0 || nbytes >= sizeof(envval)) {
176     		nbytes = 0;
177     	}
178     	envval[nbytes] = '\0';
179     	srm_printk("Loading the kernel...'%s'\n", envval);
180     
181     	/* NOTE: *no* callbacks or printouts from here on out!!! */
182     
183     	/* This is a hack, as some consoles seem to get virtual 20000000 (ie
184     	 * where the SRM console puts the kernel bootp image) memory
185     	 * overlapping physical memory where the kernel wants to be put,
186     	 * which causes real problems when attempting to copy the former to
187     	 * the latter... :-(
188     	 *
189     	 * So, we first move the kernel virtual-to-physical way above where
190     	 * we physically want the kernel to end up, then copy it from there
191     	 * to its final resting place... ;-}
192     	 *
193     	 * Sigh...  */
194     
195     #ifdef INITRD_SIZE
196     	load(initrd_start, KERNEL_ORIGIN+KERNEL_SIZE, INITRD_SIZE);
197     #endif
198             load(START_ADDR+(4*KERNEL_SIZE), KERNEL_ORIGIN, KERNEL_SIZE);
199             load(START_ADDR, START_ADDR+(4*KERNEL_SIZE), KERNEL_SIZE);
200     
201     	memset((char*)ZERO_PGE, 0, PAGE_SIZE);
202     	strcpy((char*)ZERO_PGE, envval);
203     #ifdef INITRD_SIZE
204     	((long *)(ZERO_PGE+256))[0] = initrd_start;
205     	((long *)(ZERO_PGE+256))[1] = INITRD_SIZE;
206     #endif
207     
208     	runkernel();
209     }
210