init.c 9.2 KB

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