init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/memblock.h> /* max_low_pfn */
  21. #include <linux/vmalloc.h>
  22. #include <linux/gfp.h>
  23. #include <linux/uaccess.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. p4d_t *p4d;
  127. pud_t *pud;
  128. pmd_t *pmd;
  129. void *two_pages;
  130. /* Starting at the HWRPB, locate the CRB. */
  131. crb = (struct crb_struct *)((char *)hwrpb + hwrpb->crb_offset);
  132. if (alpha_using_srm) {
  133. /* Tell the console whither it is to be remapped. */
  134. if (srm_fixup(VMALLOC_START, (unsigned long)hwrpb))
  135. __halt(); /* "We're boned." --Bender */
  136. /* Edit the procedure descriptors for DISPATCH and FIXUP. */
  137. crb->dispatch_va = (struct procdesc_struct *)
  138. (VMALLOC_START + (unsigned long)crb->dispatch_va
  139. - crb->map[0].va);
  140. crb->fixup_va = (struct procdesc_struct *)
  141. (VMALLOC_START + (unsigned long)crb->fixup_va
  142. - crb->map[0].va);
  143. }
  144. switch_to_system_map();
  145. /* Allocate one PGD and one PMD. In the case of SRM, we'll need
  146. these to actually remap the console. There is an assumption
  147. here that only one of each is needed, and this allows for 8MB.
  148. On systems with larger consoles, additional pages will be
  149. allocated as needed during the mapping process.
  150. In the case of not SRM, but not CONFIG_ALPHA_LARGE_VMALLOC,
  151. we need to allocate the PGD we use for vmalloc before we start
  152. forking other tasks. */
  153. two_pages = (void *)
  154. (((unsigned long)kernel_end + ~PAGE_MASK) & PAGE_MASK);
  155. kernel_end = two_pages + 2*PAGE_SIZE;
  156. memset(two_pages, 0, 2*PAGE_SIZE);
  157. pgd = pgd_offset_k(VMALLOC_START);
  158. p4d = p4d_offset(pgd, VMALLOC_START);
  159. pud = pud_offset(p4d, VMALLOC_START);
  160. pud_set(pud, (pmd_t *)two_pages);
  161. pmd = pmd_offset(pud, VMALLOC_START);
  162. pmd_set(pmd, (pte_t *)(two_pages + PAGE_SIZE));
  163. if (alpha_using_srm) {
  164. static struct vm_struct console_remap_vm;
  165. unsigned long nr_pages = 0;
  166. unsigned long vaddr;
  167. unsigned long i, j;
  168. /* calculate needed size */
  169. for (i = 0; i < crb->map_entries; ++i)
  170. nr_pages += crb->map[i].count;
  171. /* register the vm area */
  172. console_remap_vm.flags = VM_ALLOC;
  173. console_remap_vm.size = nr_pages << PAGE_SHIFT;
  174. vm_area_register_early(&console_remap_vm, PAGE_SIZE);
  175. vaddr = (unsigned long)console_remap_vm.addr;
  176. /* Set up the third level PTEs and update the virtual
  177. addresses of the CRB entries. */
  178. for (i = 0; i < crb->map_entries; ++i) {
  179. unsigned long pfn = crb->map[i].pa >> PAGE_SHIFT;
  180. crb->map[i].va = vaddr;
  181. for (j = 0; j < crb->map[i].count; ++j) {
  182. /* Newer consoles (especially on larger
  183. systems) may require more pages of
  184. PTEs. Grab additional pages as needed. */
  185. if (pmd != pmd_offset(pud, vaddr)) {
  186. memset(kernel_end, 0, PAGE_SIZE);
  187. pmd = pmd_offset(pud, vaddr);
  188. pmd_set(pmd, (pte_t *)kernel_end);
  189. kernel_end += PAGE_SIZE;
  190. }
  191. set_pte(pte_offset_kernel(pmd, vaddr),
  192. pfn_pte(pfn, PAGE_KERNEL));
  193. pfn++;
  194. vaddr += PAGE_SIZE;
  195. }
  196. }
  197. }
  198. callback_init_done = 1;
  199. return kernel_end;
  200. }
  201. #ifndef CONFIG_DISCONTIGMEM
  202. /*
  203. * paging_init() sets up the memory map.
  204. */
  205. void __init paging_init(void)
  206. {
  207. unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
  208. unsigned long dma_pfn;
  209. dma_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  210. max_pfn = max_low_pfn;
  211. max_zone_pfn[ZONE_DMA] = dma_pfn;
  212. max_zone_pfn[ZONE_NORMAL] = max_pfn;
  213. /* Initialize mem_map[]. */
  214. free_area_init(max_zone_pfn);
  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. memblock_free_all();
  240. mem_init_print_info(NULL);
  241. }