2 * linux/arch/alpha/mm/init.c
4 * Copyright (C) 1995 Linus Torvalds
7 /* 2.3.x zone allocator, 1999 Andrea Arcangeli <andrea@suse.de> */
9 #include <linux/config.h>
10 #include <linux/pagemap.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
20 #include <linux/swap.h>
21 #include <linux/init.h>
22 #include <linux/bootmem.h> /* max_low_pfn */
23 #include <linux/vmalloc.h>
24 #include <linux/pagemap.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/pgalloc.h>
30 #include <asm/hwrpb.h>
32 #include <asm/mmu_context.h>
33 #include <asm/console.h>
36 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
38 extern void die_if_kernel(char *,struct pt_regs *,long);
40 static struct pcb_struct original_pcb;
43 pgd_alloc(struct mm_struct *mm)
47 ret = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
48 init = pgd_offset(&init_mm, 0UL);
50 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
51 memcpy (ret + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
52 (PTRS_PER_PGD - USER_PTRS_PER_PGD - 1)*sizeof(pgd_t));
54 pgd_val(ret[PTRS_PER_PGD-2]) = pgd_val(init[PTRS_PER_PGD-2]);
57 /* The last PGD entry is the VPTB self-map. */
58 pgd_val(ret[PTRS_PER_PGD-1])
59 = pte_val(mk_pte(virt_to_page(ret), PAGE_KERNEL));
65 pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
67 pte_t *pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
73 * BAD_PAGE is the page that is used for page faults when linux
74 * is out-of-memory. Older versions of linux just did a
75 * do_exit(), but using this instead means there is less risk
76 * for a process dying in kernel mode, possibly leaving an inode
79 * BAD_PAGETABLE is the accompanying page-table: it is initialized
80 * to point to BAD_PAGE entries.
82 * ZERO_PAGE is a special page that is used for zero-initialized
88 memset((void *) EMPTY_PGT, 0, PAGE_SIZE);
89 return (pmd_t *) EMPTY_PGT;
95 memset((void *) EMPTY_PGE, 0, PAGE_SIZE);
96 return pte_mkdirty(mk_pte(virt_to_page(EMPTY_PGE), PAGE_SHARED));
99 #ifndef CONFIG_DISCONTIGMEM
103 long i,free = 0,total = 0,reserved = 0;
104 long shared = 0, cached = 0;
106 printk("\nMem-info:\n");
108 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
112 if (PageReserved(mem_map+i))
114 else if (PageSwapCache(mem_map+i))
116 else if (!page_count(mem_map+i))
119 shared += page_count(mem_map + i) - 1;
121 printk("%ld pages of RAM\n",total);
122 printk("%ld free pages\n",free);
123 printk("%ld reserved pages\n",reserved);
124 printk("%ld pages shared\n",shared);
125 printk("%ld pages swap cached\n",cached);
129 static inline unsigned long
130 load_PCB(struct pcb_struct *pcb)
132 register unsigned long sp __asm__("$30");
134 return __reload_thread(pcb);
137 /* Set up initial PCB, VPTB, and other such nicities. */
140 switch_to_system_map(void)
142 unsigned long newptbr;
143 unsigned long original_pcb_ptr;
145 /* Initialize the kernel's page tables. Linux puts the vptb in
146 the last slot of the L1 page table. */
147 memset(swapper_pg_dir, 0, PAGE_SIZE);
148 newptbr = ((unsigned long) swapper_pg_dir - PAGE_OFFSET) >> PAGE_SHIFT;
149 pgd_val(swapper_pg_dir[1023]) =
150 (newptbr << 32) | pgprot_val(PAGE_KERNEL);
152 /* Set the vptb. This is often done by the bootloader, but
153 shouldn't be required. */
154 if (hwrpb->vptb != 0xfffffffe00000000UL) {
155 wrvptptr(0xfffffffe00000000UL);
156 hwrpb->vptb = 0xfffffffe00000000UL;
157 hwrpb_update_checksum(hwrpb);
160 /* Also set up the real kernel PCB while we're at it. */
161 init_thread_info.pcb.ptbr = newptbr;
162 init_thread_info.pcb.flags = 1; /* set FEN, clear everything else */
163 original_pcb_ptr = load_PCB(&init_thread_info.pcb);
166 /* Save off the contents of the original PCB so that we can
167 restore the original console's page tables for a clean reboot.
169 Note that the PCB is supposed to be a physical address, but
170 since KSEG values also happen to work, folks get confused.
173 if (original_pcb_ptr < PAGE_OFFSET) {
174 original_pcb_ptr = (unsigned long)
175 phys_to_virt(original_pcb_ptr);
177 original_pcb = *(struct pcb_struct *) original_pcb_ptr;
180 int callback_init_done;
183 callback_init(void * kernel_end)
185 struct crb_struct * crb;
190 /* Starting at the HWRPB, locate the CRB. */
191 crb = (struct crb_struct *)((char *)hwrpb + hwrpb->crb_offset);
193 if (alpha_using_srm) {
194 /* Tell the console whither it is to be remapped. */
195 if (srm_fixup(VMALLOC_START, (unsigned long)hwrpb))
196 __halt(); /* "We're boned." --Bender */
198 /* Edit the procedure descriptors for DISPATCH and FIXUP. */
199 crb->dispatch_va = (struct procdesc_struct *)
200 (VMALLOC_START + (unsigned long)crb->dispatch_va
202 crb->fixup_va = (struct procdesc_struct *)
203 (VMALLOC_START + (unsigned long)crb->fixup_va
207 switch_to_system_map();
209 /* Allocate one PGD and one PMD. In the case of SRM, we'll need
210 these to actually remap the console. There is an assumption
211 here that only one of each is needed, and this allows for 8MB.
212 On systems with larger consoles, additional pages will be
213 allocated as needed during the mapping process.
215 In the case of not SRM, but not CONFIG_ALPHA_LARGE_VMALLOC,
216 we need to allocate the PGD we use for vmalloc before we start
217 forking other tasks. */
220 (((unsigned long)kernel_end + ~PAGE_MASK) & PAGE_MASK);
221 kernel_end = two_pages + 2*PAGE_SIZE;
222 memset(two_pages, 0, 2*PAGE_SIZE);
224 pgd = pgd_offset_k(VMALLOC_START);
225 pgd_set(pgd, (pmd_t *)two_pages);
226 pmd = pmd_offset(pgd, VMALLOC_START);
227 pmd_set(pmd, (pte_t *)(two_pages + PAGE_SIZE));
229 if (alpha_using_srm) {
230 static struct vm_struct console_remap_vm;
231 unsigned long vaddr = VMALLOC_START;
234 /* Set up the third level PTEs and update the virtual
235 addresses of the CRB entries. */
236 for (i = 0; i < crb->map_entries; ++i) {
237 unsigned long pfn = crb->map[i].pa >> PAGE_SHIFT;
238 crb->map[i].va = vaddr;
239 for (j = 0; j < crb->map[i].count; ++j) {
240 /* Newer console's (especially on larger
241 systems) may require more pages of
242 PTEs. Grab additional pages as needed. */
243 if (pmd != pmd_offset(pgd, vaddr)) {
244 memset(kernel_end, 0, PAGE_SIZE);
245 pmd = pmd_offset(pgd, vaddr);
246 pmd_set(pmd, (pte_t *)kernel_end);
247 kernel_end += PAGE_SIZE;
249 set_pte(pte_offset_kernel(pmd, vaddr),
250 pfn_pte(pfn, PAGE_KERNEL));
256 /* Let vmalloc know that we've allocated some space. */
257 console_remap_vm.flags = VM_ALLOC;
258 console_remap_vm.addr = (void *) VMALLOC_START;
259 console_remap_vm.size = vaddr - VMALLOC_START;
260 vmlist = &console_remap_vm;
263 callback_init_done = 1;
268 #ifndef CONFIG_DISCONTIGMEM
270 * paging_init() sets up the memory map.
275 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
276 unsigned long dma_pfn, high_pfn;
278 dma_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
279 high_pfn = max_pfn = max_low_pfn;
281 if (dma_pfn >= high_pfn)
282 zones_size[ZONE_DMA] = high_pfn;
284 zones_size[ZONE_DMA] = dma_pfn;
285 zones_size[ZONE_NORMAL] = high_pfn - dma_pfn;
288 /* Initialize mem_map[]. */
289 free_area_init(zones_size);
291 /* Initialize the kernel's ZERO_PGE. */
292 memset((void *)ZERO_PGE, 0, PAGE_SIZE);
294 #endif /* CONFIG_DISCONTIGMEM */
296 #if defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_SRM)
298 srm_paging_stop (void)
300 /* Move the vptb back to where the SRM console expects it. */
301 swapper_pg_dir[1] = swapper_pg_dir[1023];
303 wrvptptr(0x200000000UL);
304 hwrpb->vptb = 0x200000000UL;
305 hwrpb_update_checksum(hwrpb);
307 /* Reload the page tables that the console had in use. */
308 load_PCB(&original_pcb);
313 #ifndef CONFIG_DISCONTIGMEM
315 printk_memory_info(void)
317 unsigned long codesize, reservedpages, datasize, initsize, tmp;
318 extern int page_is_ram(unsigned long) __init;
319 extern char _text, _etext, _data, _edata;
320 extern char __init_begin, __init_end;
322 /* printk all informations */
324 for (tmp = 0; tmp < max_low_pfn; tmp++)
326 * Only count reserved RAM pages
328 if (page_is_ram(tmp) && PageReserved(mem_map+tmp))
331 codesize = (unsigned long) &_etext - (unsigned long) &_text;
332 datasize = (unsigned long) &_edata - (unsigned long) &_data;
333 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
335 printk("Memory: %luk/%luk available (%luk kernel code, %luk reserved, %luk data, %luk init)\n",
336 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
337 max_mapnr << (PAGE_SHIFT-10),
339 reservedpages << (PAGE_SHIFT-10),
347 max_mapnr = num_physpages = max_low_pfn;
348 totalram_pages += free_all_bootmem();
349 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
351 printk_memory_info();
353 #endif /* CONFIG_DISCONTIGMEM */
356 free_reserved_mem(void *start, void *end)
358 void *__start = start;
359 for (; __start < end; __start += PAGE_SIZE) {
360 ClearPageReserved(virt_to_page(__start));
361 init_page_count(virt_to_page(__start));
362 free_page((long)__start);
370 extern char __init_begin, __init_end;
372 free_reserved_mem(&__init_begin, &__init_end);
373 printk ("Freeing unused kernel memory: %ldk freed\n",
374 (&__init_end - &__init_begin) >> 10);
377 #ifdef CONFIG_BLK_DEV_INITRD
379 free_initrd_mem(unsigned long start, unsigned long end)
381 free_reserved_mem((void *)start, (void *)end);
382 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);