init.c 7.8 KB

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