init.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * linux/arch/alpha/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. /* 2.3.x zone allocator, 1999 Andrea Arcangeli <andrea@suse.de> */
  7. #include <linux/pagemap.h>
  8. #include <linux/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/mman.h>
  16. #include <linux/mm.h>
  17. #include <linux/swap.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h> /* max_low_pfn */
  20. #include <linux/vmalloc.h>
  21. #include <linux/gfp.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/hwrpb.h>
  26. #include <asm/dma.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/console.h>
  29. #include <asm/tlb.h>
  30. #include <asm/setup.h>
  31. #include <asm/sections.h>
  32. extern void die_if_kernel(char *,struct pt_regs *,long);
  33. static struct pcb_struct original_pcb;
  34. pgd_t *
  35. pgd_alloc(struct mm_struct *mm)
  36. {
  37. pgd_t *ret, *init;
  38. ret = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  39. init = pgd_offset(&init_mm, 0UL);
  40. if (ret) {
  41. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  42. memcpy (ret + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
  43. (PTRS_PER_PGD - USER_PTRS_PER_PGD - 1)*sizeof(pgd_t));
  44. #else
  45. pgd_val(ret[PTRS_PER_PGD-2]) = pgd_val(init[PTRS_PER_PGD-2]);
  46. #endif
  47. /* The last PGD entry is the VPTB self-map. */
  48. pgd_val(ret[PTRS_PER_PGD-1])
  49. = pte_val(mk_pte(virt_to_page(ret), PAGE_KERNEL));
  50. }
  51. return ret;
  52. }
  53. /*
  54. * BAD_PAGE is the page that is used for page faults when linux
  55. * is out-of-memory. Older versions of linux just did a
  56. * do_exit(), but using this instead means there is less risk
  57. * for a process dying in kernel mode, possibly leaving an inode
  58. * unused etc..
  59. *
  60. * BAD_PAGETABLE is the accompanying page-table: it is initialized
  61. * to point to BAD_PAGE entries.
  62. *
  63. * ZERO_PAGE is a special page that is used for zero-initialized
  64. * data and COW.
  65. */
  66. pmd_t *
  67. __bad_pagetable(void)
  68. {
  69. memset((void *) EMPTY_PGT, 0, PAGE_SIZE);
  70. return (pmd_t *) EMPTY_PGT;
  71. }
  72. pte_t
  73. __bad_page(void)
  74. {
  75. memset((void *) EMPTY_PGE, 0, PAGE_SIZE);
  76. return pte_mkdirty(mk_pte(virt_to_page(EMPTY_PGE), PAGE_SHARED));
  77. }
  78. static inline unsigned long
  79. load_PCB(struct pcb_struct *pcb)
  80. {
  81. register unsigned long sp __asm__("$30");
  82. pcb->ksp = sp;
  83. return __reload_thread(pcb);
  84. }
  85. /* Set up initial PCB, VPTB, and other such nicities. */
  86. static inline void
  87. switch_to_system_map(void)
  88. {
  89. unsigned long newptbr;
  90. unsigned long original_pcb_ptr;
  91. /* Initialize the kernel's page tables. Linux puts the vptb in
  92. the last slot of the L1 page table. */
  93. memset(swapper_pg_dir, 0, PAGE_SIZE);
  94. newptbr = ((unsigned long) swapper_pg_dir - PAGE_OFFSET) >> PAGE_SHIFT;
  95. pgd_val(swapper_pg_dir[1023]) =
  96. (newptbr << 32) | pgprot_val(PAGE_KERNEL);
  97. /* Set the vptb. This is often done by the bootloader, but
  98. shouldn't be required. */
  99. if (hwrpb->vptb != 0xfffffffe00000000UL) {
  100. wrvptptr(0xfffffffe00000000UL);
  101. hwrpb->vptb = 0xfffffffe00000000UL;
  102. hwrpb_update_checksum(hwrpb);
  103. }
  104. /* Also set up the real kernel PCB while we're at it. */
  105. init_thread_info.pcb.ptbr = newptbr;
  106. init_thread_info.pcb.flags = 1; /* set FEN, clear everything else */
  107. original_pcb_ptr = load_PCB(&init_thread_info.pcb);
  108. tbia();
  109. /* Save off the contents of the original PCB so that we can
  110. restore the original console's page tables for a clean reboot.
  111. Note that the PCB is supposed to be a physical address, but
  112. since KSEG values also happen to work, folks get confused.
  113. Check this here. */
  114. if (original_pcb_ptr < PAGE_OFFSET) {
  115. original_pcb_ptr = (unsigned long)
  116. phys_to_virt(original_pcb_ptr);
  117. }
  118. original_pcb = *(struct pcb_struct *) original_pcb_ptr;
  119. }
  120. int callback_init_done;
  121. void * __init
  122. callback_init(void * kernel_end)
  123. {
  124. struct crb_struct * crb;
  125. pgd_t *pgd;
  126. pmd_t *pmd;
  127. void *two_pages;
  128. /* Starting at the HWRPB, locate the CRB. */
  129. crb = (struct crb_struct *)((char *)hwrpb + hwrpb->crb_offset);
  130. if (alpha_using_srm) {
  131. /* Tell the console whither it is to be remapped. */
  132. if (srm_fixup(VMALLOC_START, (unsigned long)hwrpb))
  133. __halt(); /* "We're boned." --Bender */
  134. /* Edit the procedure descriptors for DISPATCH and FIXUP. */
  135. crb->dispatch_va = (struct procdesc_struct *)
  136. (VMALLOC_START + (unsigned long)crb->dispatch_va
  137. - crb->map[0].va);
  138. crb->fixup_va = (struct procdesc_struct *)
  139. (VMALLOC_START + (unsigned long)crb->fixup_va
  140. - crb->map[0].va);
  141. }
  142. switch_to_system_map();
  143. /* Allocate one PGD and one PMD. In the case of SRM, we'll need
  144. these to actually remap the console. There is an assumption
  145. here that only one of each is needed, and this allows for 8MB.
  146. On systems with larger consoles, additional pages will be
  147. allocated as needed during the mapping process.
  148. In the case of not SRM, but not CONFIG_ALPHA_LARGE_VMALLOC,
  149. we need to allocate the PGD we use for vmalloc before we start
  150. forking other tasks. */
  151. two_pages = (void *)
  152. (((unsigned long)kernel_end + ~PAGE_MASK) & PAGE_MASK);
  153. kernel_end = two_pages + 2*PAGE_SIZE;
  154. memset(two_pages, 0, 2*PAGE_SIZE);
  155. pgd = pgd_offset_k(VMALLOC_START);
  156. pgd_set(pgd, (pmd_t *)two_pages);
  157. pmd = pmd_offset(pgd, VMALLOC_START);
  158. pmd_set(pmd, (pte_t *)(two_pages + PAGE_SIZE));
  159. if (alpha_using_srm) {
  160. static struct vm_struct console_remap_vm;
  161. unsigned long nr_pages = 0;
  162. unsigned long vaddr;
  163. unsigned long i, j;
  164. /* calculate needed size */
  165. for (i = 0; i < crb->map_entries; ++i)
  166. nr_pages += crb->map[i].count;
  167. /* register the vm area */
  168. console_remap_vm.flags = VM_ALLOC;
  169. console_remap_vm.size = nr_pages << PAGE_SHIFT;
  170. vm_area_register_early(&console_remap_vm, PAGE_SIZE);
  171. vaddr = (unsigned long)console_remap_vm.addr;
  172. /* Set up the third level PTEs and update the virtual
  173. addresses of the CRB entries. */
  174. for (i = 0; i < crb->map_entries; ++i) {
  175. unsigned long pfn = crb->map[i].pa >> PAGE_SHIFT;
  176. crb->map[i].va = vaddr;
  177. for (j = 0; j < crb->map[i].count; ++j) {
  178. /* Newer consoles (especially on larger
  179. systems) may require more pages of
  180. PTEs. Grab additional pages as needed. */
  181. if (pmd != pmd_offset(pgd, vaddr)) {
  182. memset(kernel_end, 0, PAGE_SIZE);
  183. pmd = pmd_offset(pgd, vaddr);
  184. pmd_set(pmd, (pte_t *)kernel_end);
  185. kernel_end += PAGE_SIZE;
  186. }
  187. set_pte(pte_offset_kernel(pmd, vaddr),
  188. pfn_pte(pfn, PAGE_KERNEL));
  189. pfn++;
  190. vaddr += PAGE_SIZE;
  191. }
  192. }
  193. }
  194. callback_init_done = 1;
  195. return kernel_end;
  196. }
  197. #ifndef CONFIG_DISCONTIGMEM
  198. /*
  199. * paging_init() sets up the memory map.
  200. */
  201. void __init paging_init(void)
  202. {
  203. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  204. unsigned long dma_pfn, high_pfn;
  205. dma_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  206. high_pfn = max_pfn = max_low_pfn;
  207. if (dma_pfn >= high_pfn)
  208. zones_size[ZONE_DMA] = high_pfn;
  209. else {
  210. zones_size[ZONE_DMA] = dma_pfn;
  211. zones_size[ZONE_NORMAL] = high_pfn - dma_pfn;
  212. }
  213. /* Initialize mem_map[]. */
  214. free_area_init(zones_size);
  215. /* Initialize the kernel's ZERO_PGE. */
  216. memset((void *)ZERO_PGE, 0, PAGE_SIZE);
  217. }
  218. #endif /* CONFIG_DISCONTIGMEM */
  219. #if defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_SRM)
  220. void
  221. srm_paging_stop (void)
  222. {
  223. /* Move the vptb back to where the SRM console expects it. */
  224. swapper_pg_dir[1] = swapper_pg_dir[1023];
  225. tbia();
  226. wrvptptr(0x200000000UL);
  227. hwrpb->vptb = 0x200000000UL;
  228. hwrpb_update_checksum(hwrpb);
  229. /* Reload the page tables that the console had in use. */
  230. load_PCB(&original_pcb);
  231. tbia();
  232. }
  233. #endif
  234. void __init
  235. mem_init(void)
  236. {
  237. set_max_mapnr(max_low_pfn);
  238. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
  239. free_all_bootmem();
  240. mem_init_print_info(NULL);
  241. }
  242. void
  243. free_initmem(void)
  244. {
  245. free_initmem_default(-1);
  246. }
  247. #ifdef CONFIG_BLK_DEV_INITRD
  248. void
  249. free_initrd_mem(unsigned long start, unsigned long end)
  250. {
  251. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  252. }
  253. #endif