init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include "../kernel/proto.h"
  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_alloc(mm, 0);
  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. static inline unsigned long
  54. load_PCB(struct pcb_struct *pcb)
  55. {
  56. register unsigned long sp __asm__("$30");
  57. pcb->ksp = sp;
  58. return __reload_thread(pcb);
  59. }
  60. /* Set up initial PCB, VPTB, and other such nicities. */
  61. static inline void
  62. switch_to_system_map(void)
  63. {
  64. unsigned long newptbr;
  65. unsigned long original_pcb_ptr;
  66. /* Initialize the kernel's page tables. Linux puts the vptb in
  67. the last slot of the L1 page table. */
  68. memset(swapper_pg_dir, 0, PAGE_SIZE);
  69. newptbr = ((unsigned long) swapper_pg_dir - PAGE_OFFSET) >> PAGE_SHIFT;
  70. pgd_val(swapper_pg_dir[1023]) =
  71. (newptbr << 32) | pgprot_val(PAGE_KERNEL);
  72. /* Set the vptb. This is often done by the bootloader, but
  73. shouldn't be required. */
  74. if (hwrpb->vptb != 0xfffffffe00000000UL) {
  75. wrvptptr(0xfffffffe00000000UL);
  76. hwrpb->vptb = 0xfffffffe00000000UL;
  77. hwrpb_update_checksum(hwrpb);
  78. }
  79. /* Also set up the real kernel PCB while we're at it. */
  80. init_thread_info.pcb.ptbr = newptbr;
  81. init_thread_info.pcb.flags = 1; /* set FEN, clear everything else */
  82. original_pcb_ptr = load_PCB(&init_thread_info.pcb);
  83. tbia();
  84. /* Save off the contents of the original PCB so that we can
  85. restore the original console's page tables for a clean reboot.
  86. Note that the PCB is supposed to be a physical address, but
  87. since KSEG values also happen to work, folks get confused.
  88. Check this here. */
  89. if (original_pcb_ptr < PAGE_OFFSET) {
  90. original_pcb_ptr = (unsigned long)
  91. phys_to_virt(original_pcb_ptr);
  92. }
  93. original_pcb = *(struct pcb_struct *) original_pcb_ptr;
  94. }
  95. int callback_init_done;
  96. void * __init
  97. callback_init(void * kernel_end)
  98. {
  99. struct crb_struct * crb;
  100. pgd_t *pgd;
  101. p4d_t *p4d;
  102. pud_t *pud;
  103. pmd_t *pmd;
  104. void *two_pages;
  105. /* Starting at the HWRPB, locate the CRB. */
  106. crb = (struct crb_struct *)((char *)hwrpb + hwrpb->crb_offset);
  107. if (alpha_using_srm) {
  108. /* Tell the console whither it is to be remapped. */
  109. if (srm_fixup(VMALLOC_START, (unsigned long)hwrpb))
  110. __halt(); /* "We're boned." --Bender */
  111. /* Edit the procedure descriptors for DISPATCH and FIXUP. */
  112. crb->dispatch_va = (struct procdesc_struct *)
  113. (VMALLOC_START + (unsigned long)crb->dispatch_va
  114. - crb->map[0].va);
  115. crb->fixup_va = (struct procdesc_struct *)
  116. (VMALLOC_START + (unsigned long)crb->fixup_va
  117. - crb->map[0].va);
  118. }
  119. switch_to_system_map();
  120. /* Allocate one PGD and one PMD. In the case of SRM, we'll need
  121. these to actually remap the console. There is an assumption
  122. here that only one of each is needed, and this allows for 8MB.
  123. On systems with larger consoles, additional pages will be
  124. allocated as needed during the mapping process.
  125. In the case of not SRM, but not CONFIG_ALPHA_LARGE_VMALLOC,
  126. we need to allocate the PGD we use for vmalloc before we start
  127. forking other tasks. */
  128. two_pages = (void *)
  129. (((unsigned long)kernel_end + ~PAGE_MASK) & PAGE_MASK);
  130. kernel_end = two_pages + 2*PAGE_SIZE;
  131. memset(two_pages, 0, 2*PAGE_SIZE);
  132. pgd = pgd_offset_k(VMALLOC_START);
  133. p4d = p4d_offset(pgd, VMALLOC_START);
  134. pud = pud_offset(p4d, VMALLOC_START);
  135. pud_set(pud, (pmd_t *)two_pages);
  136. pmd = pmd_offset(pud, VMALLOC_START);
  137. pmd_set(pmd, (pte_t *)(two_pages + PAGE_SIZE));
  138. if (alpha_using_srm) {
  139. static struct vm_struct console_remap_vm;
  140. unsigned long nr_pages = 0;
  141. unsigned long vaddr;
  142. unsigned long i, j;
  143. /* calculate needed size */
  144. for (i = 0; i < crb->map_entries; ++i)
  145. nr_pages += crb->map[i].count;
  146. /* register the vm area */
  147. console_remap_vm.flags = VM_ALLOC;
  148. console_remap_vm.size = nr_pages << PAGE_SHIFT;
  149. vm_area_register_early(&console_remap_vm, PAGE_SIZE);
  150. vaddr = (unsigned long)console_remap_vm.addr;
  151. /* Set up the third level PTEs and update the virtual
  152. addresses of the CRB entries. */
  153. for (i = 0; i < crb->map_entries; ++i) {
  154. unsigned long pfn = crb->map[i].pa >> PAGE_SHIFT;
  155. crb->map[i].va = vaddr;
  156. for (j = 0; j < crb->map[i].count; ++j) {
  157. /* Newer consoles (especially on larger
  158. systems) may require more pages of
  159. PTEs. Grab additional pages as needed. */
  160. if (pmd != pmd_offset(pud, vaddr)) {
  161. memset(kernel_end, 0, PAGE_SIZE);
  162. pmd = pmd_offset(pud, vaddr);
  163. pmd_set(pmd, (pte_t *)kernel_end);
  164. kernel_end += PAGE_SIZE;
  165. }
  166. set_pte(pte_offset_kernel(pmd, vaddr),
  167. pfn_pte(pfn, PAGE_KERNEL));
  168. pfn++;
  169. vaddr += PAGE_SIZE;
  170. }
  171. }
  172. }
  173. callback_init_done = 1;
  174. return kernel_end;
  175. }
  176. void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
  177. {
  178. unsigned long dma_pfn;
  179. dma_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  180. max_pfn = max_low_pfn;
  181. max_zone_pfn[ZONE_DMA] = dma_pfn;
  182. max_zone_pfn[ZONE_NORMAL] = max_pfn;
  183. }
  184. /*
  185. * paging_init() initializes the kernel's ZERO_PGE.
  186. */
  187. void __init paging_init(void)
  188. {
  189. memset(absolute_pointer(ZERO_PGE), 0, PAGE_SIZE);
  190. }
  191. #if defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_SRM)
  192. void
  193. srm_paging_stop (void)
  194. {
  195. /* Move the vptb back to where the SRM console expects it. */
  196. swapper_pg_dir[1] = swapper_pg_dir[1023];
  197. tbia();
  198. wrvptptr(0x200000000UL);
  199. hwrpb->vptb = 0x200000000UL;
  200. hwrpb_update_checksum(hwrpb);
  201. /* Reload the page tables that the console had in use. */
  202. load_PCB(&original_pcb);
  203. tbia();
  204. }
  205. #endif
  206. static const pgprot_t protection_map[16] = {
  207. [VM_NONE] = _PAGE_P(_PAGE_FOE | _PAGE_FOW |
  208. _PAGE_FOR),
  209. [VM_READ] = _PAGE_P(_PAGE_FOE | _PAGE_FOW),
  210. [VM_WRITE] = _PAGE_P(_PAGE_FOE),
  211. [VM_WRITE | VM_READ] = _PAGE_P(_PAGE_FOE),
  212. [VM_EXEC] = _PAGE_P(_PAGE_FOW | _PAGE_FOR),
  213. [VM_EXEC | VM_READ] = _PAGE_P(_PAGE_FOW),
  214. [VM_EXEC | VM_WRITE] = _PAGE_P(0),
  215. [VM_EXEC | VM_WRITE | VM_READ] = _PAGE_P(0),
  216. [VM_SHARED] = _PAGE_S(_PAGE_FOE | _PAGE_FOW |
  217. _PAGE_FOR),
  218. [VM_SHARED | VM_READ] = _PAGE_S(_PAGE_FOE | _PAGE_FOW),
  219. [VM_SHARED | VM_WRITE] = _PAGE_S(_PAGE_FOE),
  220. [VM_SHARED | VM_WRITE | VM_READ] = _PAGE_S(_PAGE_FOE),
  221. [VM_SHARED | VM_EXEC] = _PAGE_S(_PAGE_FOW | _PAGE_FOR),
  222. [VM_SHARED | VM_EXEC | VM_READ] = _PAGE_S(_PAGE_FOW),
  223. [VM_SHARED | VM_EXEC | VM_WRITE] = _PAGE_S(0),
  224. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = _PAGE_S(0)
  225. };
  226. DECLARE_VM_GET_PAGE_PROT