init.c 9.9 KB

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