init.c 9.9 KB

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